D. The Fair Nut and the Best Path(树形dp)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/84980906

D. The Fair Nut and the Best Path

http://codeforces.com/contest/1084/problem/D

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut is going to travel to the Tree Country, in which there are n

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 u and go by a simple path to city v

. 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 wi

liters of gasoline in the i

-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 n

(1≤n≤3⋅105

) — the number of cities.

The second line contains n

integers w1,w2,…,wn (0≤wi≤109

) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n−1

lines describes road and contains three integers u, v, c (1≤u,v≤n, 1≤c≤109, u≠v), where u and v — cities that are connected by this road and c

 — 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

Copy

3
1 3 3
1 2 2
1 3 2

Output

Copy

3

Input

Copy

5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1

Output

Copy

7

Note

The optimal way in the first example is 2→1→3

.

The optimal way in the second example is 2→4

.

题意:

就是点是可以加油的,然后线上是耗油的。问任意两点之间的所有路径的和最大的值。

思路:经典的树形dp。

用dp[u]表示以u为起点的最大值,dp_z[u]代表,u作为中间点的最大值。那么状态转移方程自然就出来了:

对于点u,从所有儿子中找到一个最大的dp[ son1 ],次大的dp[ son2 ],
dp_z[ u ]=a[ u ]+dp[ son1 ]+dist( u,son1 )+dp[ son2 ]+dist( u,son2 ),
dp[ u ]=a[ u ]+dp[ son1 ]+dist( u,son1 )

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=300000+100;
typedef long long LL;
vector<int>G[maxn],dist[maxn];
LL a[maxn],dp[maxn],dp_z[maxn];
int w;
int u,v;
LL ans;
void dfs(int u,int fa){
    LL mx1=0;///最大
    LL mx2=0;///次大
    ///dp[u]=a[u];///u作为起点
    ///dp_z[u]=a[u];///作为中间点
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==fa)
            continue;
        dfs(v,u);
        if(dp[v]-dist[u][i]>mx1){
            mx2=mx1;
            mx1=dp[v]-dist[u][i];
        }
        else if(dp[v]-dist[u][i]==mx1){
            mx2=mx1;
        }
        else{
            if(dp[v]-dist[u][i]>mx2)
                mx2=dp[v]-dist[u][i];
        }///更新最大和次大值
    }
    dp_z[u]=a[u]+mx1+mx2;
    dp[u]=a[u]+mx1;
    ans=max(dp_z[u],ans);
    ans=max(dp[u],ans);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
            ans=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        for(int i=1;i<=n-1;i++){
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back(v);
            G[v].push_back(u);
            dist[u].push_back(w);
            dist[v].push_back(w);
        }
        dfs(1,-1);
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/84980906