#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) (ll)__builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e4 + 10;
int dp[N][N][2], sz[N];
vector <pair <int, int>> ed[N];
int n, k;
void minimize(int &a, int b) {
if (a > b) {
a = b;
}
}
void dfs(int u, int v) {
sz[u] = 1;
dp[u][0][1] = dp[u][1][0] = dp[u][0][0] = dp[u][1][1] = 0;
for (auto x : ed[u]) {
if (x.f == v)
continue;
dfs(x.f, u);
sz[u] += sz[x.f];
for (int a = min(k, sz[u]); a >= 2; a--) {
for (int b = max(0, a - sz[u] + sz[x.f]); b <= min(sz[x.f], a); b++) {
minimize(dp[u][a][0], dp[x.f][b][0] + dp[u][a - b][0] + x.s * 2);
minimize(dp[u][a][1], dp[x.f][b][1] + dp[u][a - b][0] + x.s);
minimize(dp[u][a][1], dp[x.f][b][0] + dp[u][a - b][1] + x.s * 2);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
mem(dp, 0x3f);
int x;
cin >> n >> k >> x;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
ed[a].pb({b, c});
ed[b].pb({a, c});
}
dfs(x, x);
cout << min(dp[x][k][1], dp[x][k][0]) << '\n';
return 0;
}
/*
11 8 3
1 3 3
3 2 5
6 4 5
1 11 3
9 1 2
9 10 2
3 7 10
6 7 1
7 8 1
7 5 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
784976 KB |
Output is correct |
2 |
Correct |
268 ms |
784976 KB |
Output is correct |
3 |
Correct |
267 ms |
785052 KB |
Output is correct |
4 |
Correct |
266 ms |
784980 KB |
Output is correct |
5 |
Correct |
276 ms |
784872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
785232 KB |
Output is correct |
2 |
Correct |
269 ms |
785216 KB |
Output is correct |
3 |
Correct |
278 ms |
786208 KB |
Output is correct |
4 |
Correct |
279 ms |
785376 KB |
Output is correct |
5 |
Correct |
275 ms |
785396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
785232 KB |
Output is correct |
2 |
Correct |
269 ms |
785216 KB |
Output is correct |
3 |
Correct |
278 ms |
786208 KB |
Output is correct |
4 |
Correct |
279 ms |
785376 KB |
Output is correct |
5 |
Correct |
275 ms |
785396 KB |
Output is correct |
6 |
Correct |
276 ms |
785216 KB |
Output is correct |
7 |
Correct |
276 ms |
785816 KB |
Output is correct |
8 |
Correct |
270 ms |
785620 KB |
Output is correct |
9 |
Correct |
277 ms |
785452 KB |
Output is correct |
10 |
Correct |
297 ms |
785276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
784976 KB |
Output is correct |
2 |
Correct |
268 ms |
784976 KB |
Output is correct |
3 |
Correct |
267 ms |
785052 KB |
Output is correct |
4 |
Correct |
266 ms |
784980 KB |
Output is correct |
5 |
Correct |
276 ms |
784872 KB |
Output is correct |
6 |
Correct |
289 ms |
785232 KB |
Output is correct |
7 |
Correct |
269 ms |
785216 KB |
Output is correct |
8 |
Correct |
278 ms |
786208 KB |
Output is correct |
9 |
Correct |
279 ms |
785376 KB |
Output is correct |
10 |
Correct |
275 ms |
785396 KB |
Output is correct |
11 |
Correct |
276 ms |
785216 KB |
Output is correct |
12 |
Correct |
276 ms |
785816 KB |
Output is correct |
13 |
Correct |
270 ms |
785620 KB |
Output is correct |
14 |
Correct |
277 ms |
785452 KB |
Output is correct |
15 |
Correct |
297 ms |
785276 KB |
Output is correct |
16 |
Correct |
304 ms |
785312 KB |
Output is correct |
17 |
Correct |
339 ms |
785400 KB |
Output is correct |
18 |
Correct |
298 ms |
785492 KB |
Output is correct |
19 |
Correct |
317 ms |
785232 KB |
Output is correct |
20 |
Correct |
298 ms |
785356 KB |
Output is correct |
21 |
Correct |
412 ms |
785744 KB |
Output is correct |
22 |
Correct |
355 ms |
785600 KB |
Output is correct |
23 |
Correct |
470 ms |
785760 KB |
Output is correct |
24 |
Correct |
362 ms |
785628 KB |
Output is correct |
25 |
Correct |
441 ms |
785972 KB |
Output is correct |