-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrepidclimber.cpp
77 lines (72 loc) · 1.62 KB
/
Intrepidclimber.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
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100005;
const long long INF = 1000000000000000;
int N, F, A, B, C;
bool friends[MAX];
vector<int> graph[MAX], out[MAX], fullcost[MAX], cost[MAX];
bool simplify(int u) {
bool ans = false;
for (int v = 0; v < graph[u].size(); v++) {
bool curr = simplify(graph[u][v]);
if (curr) {
out[u].push_back(graph[u][v]);
cost[u].push_back(fullcost[u][v]);
}
ans = ans || curr;
}
return ans || friends[u];
}
long long dfs_return(int u) {
long long ans = 0;
for (int v = 0; v < out[u].size(); v++) {
ans += dfs_return(out[u][v]) + cost[u][v];
}
return ans;
}
long long dfs_stay(int u) {
if (out[u].size() == 0) {
return 0;
} else if (out[u].size() == 1) {
return dfs_stay(out[u][0]);
} else {
long long ans = INF, total = 0;
for (int v = 0; v < out[u].size(); v++) {
long long ret = dfs_return(out[u][v]) + cost[u][v];
long long stay = dfs_stay(out[u][v]);
total += ret;
ans = min(ans, stay - ret);
}
return total + ans;
}
}
int main() {
// until EOF
while (cin >> N && cin >> F) {
// reset data structures
for (int i = 0; i < N; i++) {
graph[i].clear();
out[i].clear();
fullcost[i].clear();
cost[i].clear();
friends[i] = false;
}
// read graph
for (int i = 0; i < N-1; i++) {
cin >> A; cin >> B; cin >> C;
graph[A-1].push_back(B-1);
fullcost[A-1].push_back(C);
}
// read friends
for (int i = 0; i < F; i++) {
int f; cin >> f;
friends[f-1] = true;
}
// simplify graph so algorithm is simpler
simplify(0);
// solve problem and print ans
cout << dfs_stay(0) << endl;
}
}