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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <bits/stdc++.h> #define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back #define DEBUG(x) cout << #x << " = " << x << endl; using namespace std; typedef long long LL; typedef pair <int, int> pii; template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline T read () { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; } inline void proc_status () { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl; } const int Maxn = 50 + 5; const int inf = 0x3f3f3f3f; int N, S, M; int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1]; inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; } int is_leaf[Maxn], size[Maxn]; inline void dfs (int x, int f) { int cnt = 0; for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; ++cnt; if (y == f) continue; dfs (y, x); size[x] += size[y]; } if (cnt == 1) is_leaf[x] = 1; } int Dp[Maxn][Maxn][Maxn][Maxn], A[Maxn][Maxn]; inline int get_dp (int x, int to, int s1, int s2) { if (s1 + s2 == 0) return 0; if (!s1) return inf; if (Dp[x][to][s1][s2] < 1e9) return Dp[x][to][s1][s2]; if (is_leaf[to]) return Dp[x][to][s1][s2] = get_dp (to, x, s2, 0) + A[x][to]; int f[Maxn]; memset (f, 0, sizeof f); f[0] = inf; for (int i = Begin[to]; i; i = Next[i]) { int y = To[i]; if (y == x) continue; for (int j = s1; j >= 0; --j) for (int k = 0; k <= j; ++k) Chkmax (f[j], min (f[j - k], get_dp (to, y, k, s1 + s2 - k))); } return Dp[x][to][s1][s2] = f[s1] + A[x][to]; } inline void Solve () { dfs (S, 0); memset (Dp, 0x3f, sizeof Dp); int ans = inf; for (int i = Begin[S]; i; i = Next[i]) { int y = To[i]; Chkmin (ans, get_dp (S, y, size[y], M - size[y])); } cout << ans << endl; } inline void Input () { N = read<int>(); for (int i = 1; i < N; ++i) { int x = read<int>(), y = read<int>(), z = read<int>(); A[x][y] = A[y][x] = z; add_edge (x, y); add_edge (y, x); } S = read<int>(), M = read<int>(); for (int i = 1; i <= M; ++i) ++size[read<int>()]; } int main() { #ifndef ONLINE_JUDGE freopen("E.in", "r", stdin); freopen("E.out", "w", stdout); #endif Input (); Solve (); return 0; }
|