transaction transaction transaction(树形DP)

描述

传送门:2017 ACM/ICPC Asia Regional Shenyang Online

Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named “the Man Who Changed China”. Of course, Kelukin wouldn’t miss this chance to make money, but he doesn’t have this book. So he has to choose two city to buy and sell.
As we know, the price of this book was different in each city. It is ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are $n−1$ roads connecting $n$ cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.

Input

The first line contains an integer $T$ ($1≤T≤10$) , the number of test cases.
For each test case:
first line contains an integer $n$ $(2≤n≤100000)$ means the number of cities;
second line contains $n$ numbers, the ith number means the prices in ith city; $(1≤Price≤10000)$
then follows $n−1$ lines, each contains three numbers $x$, $y$ and $z$ which means there exists $a$ road between $x$ and $y$, the distance is $zkm$ $(1≤z≤1000)$.

Output

For each test case, output a single number in a line: the maximum money he can get.

Sample Input

1
2
3
4
5
6
1
4
10 40 15 30
1 2 30
1 3 2
3 4 10

Sample Output

1
8

思路

  • 经典的树形dp。设$dp[u][0]$表示当前结点卖出或走到子树卖出的最少花费,$dp[u][1]$表示当前结点买入或子树买入并走到当前结点的最高价格。最终答案即$dp[u][1]-dp[u][0]$的最大值。只要对整个树dfs一次就好。
  • 感谢队友大佬帮忙推出的转移方程

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin \
ios_base::sync_with_stdio(0); \
cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int maxn = 1e5 + 5;
int val[maxn];
int dp[maxn][2];
vector<PII> G[maxn];
void init(int n)
{
for (int i = 0; i <= n; i++)
G[i].clear();
}

void addedge(int u, int v, int w)
{
G[u].pb(mp(v, w));
G[v].pb(mp(u, w));
}

void dfs(int u, int p)
{
dp[u][0] = val[u];
dp[u][1] = val[u];
for (int i = 0; i < G[u].size(); i++)
{
int v = G[u][i].X;
int w = G[u][i].Y;
if (p == v) continue;
dfs(v, u);
dp[u][0] = min(dp[u][0], dp[v][0] + w);
dp[u][1] = max(dp[u][1], dp[v][1] - w);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
init(n);
for (int i = 1; i <= n; i++)
scanf("%d", val + i);
for (int i = 1; i < n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
dfs(1, 0);
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, dp[i][1] - dp[i][0]);
printf("%d\n", ans);
}
return 0;
}
捐助作者
0%