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
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args<< " -> "; err(args); } while (false) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 1E6 + 100; const LL INF = 1E18; LL f[maxn], g[maxn]; // f: include, g: exclude struct E { LL to, d; }; bool b[maxn]; vector<E> G[maxn];
void go(LL u, LL fa) { LL s = 0, m = 0; for (E& e: G[u]) { LL v = e.to; if (v == fa) continue; go(v, u); LL t = min(f[v], g[v] + e.d); s += t; m = max(m, t - g[v]); } if (b[u]) { f[u] = INF; g[u] = s; } else { f[u] = s; g[u] = s - m; } }
int main() { int n, k; cin >> n >> k; FOR (_, 1, n) { LL u, v, d; scanf("%lld%lld%lld", &u, &v, &d); G[u].push_back({v, d}); G[v].push_back({u, d}); } while (k--) { LL u; scanf("%lld", &u); b[u] = true; } go(1, -1); cout << g[1] << endl; }
|