hdu-5834 Magic boy Bi Luo with his excited tree(樹形dp)

題目鏈接:

Magic boy Bi Luo with his excited tree

Time Limit: 8000/4000 MS (Java/Others)????Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1037????Accepted Submission(s): 298


Problem Description
Bi Luo is a magic boy, he also has a migic tree, the tree has?N?nodes , in each node , there is a treasure, it's value is?V[i], and for each edge, there is a cost?C[i], which means every time you pass the edge?i?, you need to pay?C[i].

You may attention that every?V[i]?can be taken only once, but for some?C[i]?, you may cost severial times.

Now, Bi Luo define?ans[i]?as the most value can Bi Luo gets if Bi Luo starts at node?i.

Bi Luo is also an excited boy, now he wants to know every?ans[i], can you help him?

?

Input
First line is a positive integer?T(T104)?, represents there are?T?test cases.

Four each test:

The first line contain an integer?N(N105).

The next line contains?N?integers?V[i], which means the treasure’s value of node?i(1V[i]104).

For the next?N?1?lines, each contains three integers?u,v,c?, which means node?u?and node?v?are connected by an edge, it's cost is?c(1c104).

You can assume that the sum of?N?will not exceed?106.

?

Output
For the i-th test case , first output Case #i: in a single line , then output?N?lines , for the i-th line , output?ans[i]?in a single line.

?

Sample Input
1
5
4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2

?

Sample Output
Case #1:
15
10
14
9
15
題意:
每個節點有價值v[i]的寶物,但是任何兩個節點u,v之間的路走一次花費為w,從每個節點出發最多可以賺多少錢;
思路:
樹形dp的題目,需要記錄轉移的最大和次大,注意轉移的情況,不能寫漏了;
AC代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));typedef  long long LL;template<class T> void read(T&num) {char CH; bool F=false;for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {if(!p) { puts("0"); return; }while(p) stk[++ tp] = p%10, p/=10;while(tp) putchar(stk[tp--] + '0');putchar('\n');
}const int mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=(1<<20)+10;
const int maxn=1e5+110;
const double eps=1e-12;int n,cnt,head[maxn],a[maxn];
int down[maxn][2],up[maxn][2],max1[maxn],max2[maxn],temp[maxn],cost[maxn];
struct Edge
{int from,to,next,val;
}edge[2*maxn];
inline void add_edge(int s,int e,int va)
{edge[cnt].from=s;edge[cnt].to=e;edge[cnt].next=head[s];edge[cnt].val=va;head[s]=cnt++;
}
inline void Init()
{cnt=0;for(int i=0;i<=n;i++)head[i]=-1;
}
void dfs(int cur,int fa,int va)
{down[cur][1]=a[cur];cost[cur]=va;for(int i=head[cur];i!=-1;i=edge[i].next){int x=edge[i].to;if(x==fa)continue;dfs(x,cur,edge[i].val);if(down[x][1]-2*edge[i].val>=0)down[cur][1]+=down[x][1]-2*edge[i].val;}
}
void dfs1(int cur,int fa)
{down[cur][0]=a[cur];temp[cur]=max1[cur]=max2[cur]=0;for(int i=head[cur];i!=-1;i=edge[i].next){int x=edge[i].to;if(x==fa)continue;dfs1(x,cur);if(down[x][0]-edge[i].val>0){int t=down[cur][1];if(down[x][1]-2*edge[i].val>=0)t-=down[x][1]-2*edge[i].val;t+=down[x][0]-edge[i].val;if(t>=down[cur][0]){max2[cur]=max1[cur];temp[cur]=down[cur][0];down[cur][0]=t;max1[cur]=x;}else if(t>temp[cur]){max2[cur]=x;temp[cur]=t;}}}
}void dfs2(int cur,int fa,int va)
{up[cur][1]=0;if(down[cur][1]-2*va>=0)up[cur][1]=max(up[cur][1],down[fa][1]-down[cur][1]+2*va+up[fa][1]-2*va);else up[cur][1]=max(up[cur][1],down[fa][1]+up[fa][1]-2*va);up[cur][0]=0;if(max1[fa]==cur){int t=down[fa][0]-down[cur][0]+va;up[cur][0]=max(up[cur][0],t+up[fa][0]-va);int r=max2[fa];if(down[r][1]-2*cost[r]>0)t=t-down[r][1]+2*cost[r];t+=down[r][0]-cost[r];up[cur][0]=max(up[cur][0],t+up[fa][1]-va);}else {int t=down[fa][0];if(down[cur][1]-2*va>0)t-=down[cur][1]-2*va;up[cur][0]=max(up[cur][0],t+up[fa][1]-va);t=down[fa][1];if(down[cur][1]-2*va>0)t-=down[cur][1]-2*va;up[cur][0]=max(up[cur][0],t+up[fa][0]-va);}for(int i=head[cur];i!=-1;i=edge[i].next){int x=edge[i].to;if(x==fa)continue;dfs2(x,cur,edge[i].val);}
}int main()
{int t,Case=0;read(t);while(t--){read(n);Init();For(i,1,n)read(a[i]);int u,v,w;For(i,1,n-1){read(u);read(v);read(w);add_edge(u,v,w);add_edge(v,u,w);}dfs(1,0,0);dfs1(1,0);dfs2(1,0,0);printf("Case #%d:\n",++Case);for(int i=1;i<=n;i++)printf("%d\n",max(down[i][0]+up[i][1],down[i][1]+up[i][0]));}    return 0;
}

  

轉載于:https://www.cnblogs.com/zhangchengc919/p/5856654.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373933.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373933.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373933.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java EE過去,現在和云7

最近的JavaOne 2011的一個突出主題是下一個主要的Java EE 7版本。 正如主題發言中所述&#xff0c;有關工作正在進行中。 它將包含我們已經從先行者那里知道的28個規范以及一些新規范。 沒人可以告訴您確切的號碼&#xff0c;因為EE 7僅在“及時”完成時才會接受新的規范。 這意…

python cnn識別圖像_笨方法學習CNN圖像識別(一)—— 圖片預處理

— 全文閱讀5分鐘 —在本文中&#xff0c;你將學習到以下內容&#xff1a;通過數據增強增加樣本量調整圖片大小便于網絡訓練前言圖像識別的準備工作就是要對我們拿到手的樣本圖片進行預處理&#xff0c;具體就是數據增強和調整圖片大小&#xff0c;這些準備工作都是為訓練網絡做…

隨機數發生器

很多人喜歡用 rand()%n產生區間 [0,n]內的一個隨機整數。姑且不論這樣產生的整數是否仍然均勻分布&#xff0c;當 n大于 RAND_MAX 時&#xff0c;此法并不能得到期望的結果。由于RAND_MAX 很可能只是32767這么小&#xff0c;在使用此法時應當小心。 #include "stdio.h&quo…

Request和Response詳解

轉自&#xff1a;http://zhidao.baidu.com/link?url8BI0cjlcFdBSJKHTZlpo874eqtbTJoZfrh3miQgM_05RvSER8skPiBc1wSPZtXT8OGGCHfVXFAzAosa6E5HBl_ 內置對象request&#xff1a;請求對象request.getParameter("名字") 獲得客戶端輸入的信息***************request.get…

將Maven與Ivy集成

問題是&#xff1a;您在Ivy存儲庫中&#xff08;只有那里&#xff09;有一些資源&#xff0c;您想在基于Maven的項目中使用這些資源。 可能的解決方案&#xff1a; 由于Ivy可以輕松使用Maven風格的存儲庫&#xff08;因此&#xff0c;您的Ivy客戶端可以繼續使用Ivy并進行一些微…

用python下載辭典

用python下載詞源詞典Etymoline Online Etymology Dictionary是最好的 English 詞源詞典&#xff0c;現在來說沒有之一。但是&#xff0c;一直在PC上查單詞有時不是很方便&#xff0c;遂就想怎么才能在手機上使用。現在的手機上的詞典&#xff0c;除了BlueDict、MDict之外&…

程序員都用什么來記錄知識_1年前的小五都用 Python 來做什么?

↑ 點擊上方 “凹凸數據” 關注 星標 ~ 每天更新&#xff0c;干貨不斷 (多圖預警)注&#xff1a;這是小五一年前在知乎的回答&#xff0c;當時還只有凹凸數讀一個公眾號&#xff0c;所以很多圖片都會帶有數讀或者知乎的水印。作為一個菜鳥數據分析師&#xff0c;只會sqlpytho…

CSDN編程挑戰——《高斯公式》

高斯公式 題目詳情: 高斯在上小學時發明了等差數列求和公式:12..1005050。現在問題在于給你一個正整數n&#xff0c;問你他可以表示為多少種連續正整數之和&#xff1f;&#xff08;自身也算&#xff09;。 輸入格式&#xff1a; 多組數據&#xff0c;每組數據一行&#xff0c…

SQL-行轉列(PIVOT)實例1

--未旋轉之前的查詢結果 select s.Name ShiftName,h.BusinessEntityID,d.Name as DpartmentName from HumanResources.EmployeeDepartmentHistory h inner join HumanResources.Department d on h.DepartmentIDd.DepartmentIDinner join HumanResources.Shift s on s.ShiftIDh…

將MongoDB與Morphia結合使用

在過去的幾年中&#xff0c; NoSQL數據庫&#xff08;例如CouchDB&#xff0c;Cassandra和MongoDB&#xff09;在不需要運行傳統RDBMS的語義和開銷的應用程序中得到了普及。 我不會進入選擇NoSQL數據庫的設計決策&#xff0c;因為其他人已經做得很好&#xff0c;但是我將結合我…

webservice接口_webservice服務器端發票識別接口

關鍵詞&#xff1a;發票識別 私有云發票識別 發票識別API接口 webservice發票識別平臺發票&#xff0c;一個再也熟悉不過的財務往來憑證&#xff0c;錄入發票&#xff0c;一項讓多少財會人員頭疼的工作。過去錄入一張發票需要一個財會人員5分鐘的時間&#xff0c;那么這個人在工…

二叉樹學習——簡單入門題

入門題一&#xff1a; 輸入一顆二叉樹&#xff0c;你的任務是按從上到下、從左到右的順序輸出各個節點的值。每個節點都按照從根節點到它的移動序列給出 &#xff08;L表示左&#xff0c;R表示右&#xff09;。在輸入中&#xff0c;每個節點的左括號和右括號之間沒有空格&#…

java8-4 多態的練習以及題目

1、/* 多態練習&#xff1a;貓狗案例*/ 1 class Animal {2 public void eat(){3 System.out.println("吃飯");4 }5 }6 7 class Dog extends Animal {8 public void eat() {9 System.out.println("狗吃肉"); 10 } 11 12 public void lookDoor() { 13 Syste…

一個簡單的socket通信小demo

寫了一個socket的程序&#xff0c;可以和本地的服務器進行通信&#xff0c;要先和服務器建立鏈接&#xff0c;然后發送登錄信息&#xff0c;驗證成功&#xff0c;就可以和服務器通信了 1 頁面截圖 2 點擊鏈接服務器&#xff0c;可以鏈接服務器&#xff0c;服務器的ip地址為&…

Java并發教程– CountDownLatch

Java中的某些并發實用程序自然會比其他并發實用程序受到更多關注&#xff0c;因為它們可以解決通用問題而不是更具體的問題。 我們大多數人經常遇到執行程序服務和并發集合之類的事情。 其他實用程序不太常見&#xff0c;因此有時它們可??能會使我們逃脫&#xff0c;但是請記…

漢儀尚巍手書可以商用嗎_【商用車維修】夏天修空調可以撐起全年修車收入的一半,你會了嗎?...

更多精彩&#xff0c;請點擊上方藍字關注我們&#xff01;車載空調是炎熱的季節必不可少的利器&#xff0c;但用得多&#xff0c;毛病也多了起來&#xff0c;今天和大家分享一些空調系統的相關知識&#xff0c;助力修車師傅們來應對空調系統的相關故障問題。如何判斷制冷系統的…

CSDN編程挑戰——《-3+1》

-31 題目詳情: 有一個數列&#xff0c;所有的數都是非負整數&#xff0c;你可以進行如下方式進行一次操作&#xff08;注意一次完整的操作必須先后完成如下兩個步驟&#xff09;&#xff1a; &#xff08;1&#xff09; 任選一個不小于3的數&#xff0c;把它減少3。 &#xff…

游戲感悟

1.所謂游戲平衡&#xff0c;就是指玩家沒有最優解。 2.所謂公司的文化&#xff0c;就是指員工被公司洗腦的那些觀點(認知)。 3.人是能動的&#xff0c;擺脫平庸。轉載于:https://www.cnblogs.com/yangzhou33/p/5074509.html

Git 簡單使用

1.Git是什么 簡介&#xff1a;Git是 Linux 之父 Linus Trovalds&#xff0c;為管理 Linux 內核代碼而建立的&#xff0c;被認為是分布式版本控制工具中的頂級水準。智能、友好、強健、高效。 作用&#xff1a;新建一個分支&#xff0c;把服務器上最新版的代碼fetch下來&#x…

Vaadin附加組件和Maven

介紹 我喜歡Vaadin的 &#xff08;眾多&#xff09;一件事是它對Vaadin框架的“附加組件”社區-他們稱之為Vaadin目錄 。 “附加組件”是框架中社區貢獻的附加組件&#xff0c;可以是任何東西&#xff0c;例如從新的客戶端小部件到數據表的延遲加載容器。 我肯定會為Activiti看…