-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsterix and the Chariot Race.cpp
83 lines (65 loc) · 1.45 KB
/
Asterix and the Chariot Race.cpp
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
79
80
81
82
83
#include <bits/stdc++.h>
#define int long
using namespace std;
struct City {
int repared;
int noneed;
int need;
};
vector<vector<int>> graph;
vector<int> costs;
vector<City> dp;
void compute(int idx) {
// Base case
if(graph[idx].size() == 0) {
dp[idx] = {costs[idx],costs[idx],0};
return;
}
// Recursive case
City tmp = {0,0,0};
for(auto child: graph[idx]) {
compute(child);
}
// Case 1
tmp.repared = costs[idx];
for(auto child: graph[idx]) {
tmp.repared += min({dp[child].repared,dp[child].noneed,dp[child].need});
}
// Case 2
int min_diff = INT_MAX;
for(auto child: graph[idx]) {
tmp.noneed += min({dp[child].repared,dp[child].noneed});
min_diff = min(min_diff, dp[child].repared - dp[child].noneed);
}
tmp.noneed += max(0l,min_diff);
tmp.noneed = min(tmp.repared,tmp.noneed);
// Case 3
for(auto child: graph[idx]) {
tmp.need += min({dp[child].repared,dp[child].noneed});
}
dp[idx] = tmp;
}
void solve() {
int n;
cin >> n;
graph = vector<vector<int>>(n,vector<int>(0));
for(int i = 0; i < n-1;i++) {
int from;
int to;
cin >> from >> to;
graph[from].push_back(to);
}
costs = vector<int>(n);
for(int i = 0; i < n;i++) cin >> costs[i];
dp = vector<City>(n);
compute(0);
cout << min(dp[0].repared, dp[0].noneed) << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
solve();
}