【CodeForces - 1084D】The Fair Nut and the Best Path (树形dp)
題干:
The Fair Nut is going to travel to the Tree Country, in which there are?nn?cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city?uu?and go by a simple path to city?vv. He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex.
A filling station is located in every city. Because of strange law, Nut can buy only?wiwi?liters of gasoline in the?ii-th city. We can assume, that he has?infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in?every?visited city, even in?the first?and?the last.
He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.
Input
The first line contains a single integer?nn?(1≤n≤3?1051≤n≤3?105)?— the number of cities.
The second line contains?nn?integers?w1,w2,…,wnw1,w2,…,wn?(0≤wi≤1090≤wi≤109)?— the maximum amounts of liters of gasoline that Nut can buy in cities.
Each of the next?n?1n?1?lines describes road and contains three integers?uu,?vv,?cc?(1≤u,v≤n1≤u,v≤n,?1≤c≤1091≤c≤109,?u≠vu≠v), where?uu?and?vv?— cities that are connected by this road and?cc?— its length.
It is guaranteed that graph of road connectivity is a tree.
Output
Print one number?— the maximum amount of gasoline that he can have at the end of the path.
Examples
Input
3 1 3 3 1 2 2 1 3 2Output
3Input
5 6 3 2 5 0 1 2 10 2 3 3 2 4 1 1 5 1Output
7Note
The optimal way in the first example is?2→1→32→1→3.
The optimal way in the second example is?2→42→4.
題目大意:
每個(gè)點(diǎn)有權(quán)值,每個(gè)邊也有權(quán)值,你可以任選一條路徑使得 路徑上的點(diǎn)的點(diǎn)權(quán)和減去邊權(quán)和 最大,答案也可以只為一個(gè)點(diǎn),輸出這個(gè)最大值。
解題報(bào)告:
? 樹形dp,遍歷每個(gè)點(diǎn)的時(shí)候維護(hù)最大值和次大值,返回那個(gè)最大值然后答案可以從它本身,他和他的一棵子樹,他和他的兩棵子樹,分別轉(zhuǎn)移過(guò)來(lái)。(注意因?yàn)轭}干中權(quán)值肯定是正數(shù),所以可以直接tmp1和tmp2都賦值為0,這樣就不用更新三次maxx了。。)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 3e5 + 5; typedef pair<int,ll> PIL; vector<PIL> vv[MAX]; ll a[MAX]; ll dp[MAX]; ll maxx = -123123123123; ll dfs(int cur,int rt) {int up = vv[cur].size();ll res=a[cur],tmp1=-123123123123,tmp2=-123123123123;for(int i = 0; i<up; i++) {PIL v = vv[cur][i];if(v.first == rt) continue;ll tmp = dfs(v.first,cur) - v.second;if(tmp >= tmp1) {tmp2 = tmp1;tmp1 = tmp;}else if(tmp >= tmp2) {tmp2 = tmp;}res = max(res,tmp + a[cur]);}maxx = max(maxx,tmp1+tmp2+a[cur]);maxx = max(maxx,tmp1+a[cur]);maxx = max(maxx,a[cur]);return res; } int main() {int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);for(int u,v,i = 1; i<=n-1; i++) {ll c;scanf("%d%d%lld",&u,&v,&c);vv[u].pb(pm(v,c));vv[v].pb(pm(u,c)); }dfs(1,0);printf("%lld\n",maxx);return 0 ; } /* 2 2 2 1 2 1 */?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 1084D】The Fair Nut and the Best Path (树形dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 还剩最后几天,存量房贷将执行新贷款利率,
- 下一篇: 【HDU - 6231】K-th Num