Codeforces Round #526 (Div. 2) D

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
3
1 3 3
1 2 2
1 3 2
Output
3
Input
5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1
Output
7

题意很简单,经过一条路会失去对应的汽油,到达这个点会获得汽油。
求最大可能拿到的汽油。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<stdio.h>
#include<functional>
#include<time.h>
#pragma warning(disable:6031)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + 10;
const ll mode = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950;
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }

vector<int>G[maxn], dis[maxn];
ll d[maxn], f[maxn], a[maxn], ans = 0;

void dfs(int u, int fa)
{//非常巧妙的树状dp,数组d代表该点为途经点的最大值,f为该点为起点的最大值
	d[u] = f[u] = a[u];
	ll mx = 0, mx2 = 0;
	for (int i = 0; i < G[u].size(); i++)
	{
		int v = G[u][i];
		if (v == fa)continue;
		dfs(v, u);
		if (f[v] - dis[u][i] > mx)
			mx2 = mx, mx = f[v] - dis[u][i];
		else if (f[v] - dis[u][i] > mx2)
			mx2 = f[v] - dis[u][i];
	}
	d[u] += mx + mx2;//途经点的最大值为次大的dfs与最大的之和
	f[u] += mx;
	ans = max(ans, d[u]);
	ans = max(ans, f[u]);
}

int main()
{
	int n, u, v, w;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%I64d", &a[i]);
	for (int i = 1; i < n; i++)
	{
		scanf("%d %d %d", &u, &v, &w);
		G[u].push_back(v);
		G[v].push_back(u);
		dis[u].push_back(w);
		dis[v].push_back(w);
	}
	dfs(1, -1);
	printf("%I64d\n", ans);
}

猜你喜欢

转载自blog.csdn.net/weixin_43896680/article/details/86651687