#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e4 + 2;
int n, k, x;
int dp[N][N][2], son[N];
vector<pii> g[N];
void dfs(int u, int fa) {
son[u] = 1;
dp[u][1][0] = dp[u][1][1] = 0;
for (pii f : g[u]) {
int v = f.first, w = f.second;
if (v == fa) continue;
dfs(v, u);
son[u] += son[v];
for (int i = min(son[u], k); i >= 0; i--) {
for (int j = 0; j <= min(son[v], i - 1); j++) {
dp[u][i][0] = min(dp[u][i][0], dp[u][i - j][0] + dp[v][j][0] + w * 2);
dp[u][i][1] = min(dp[u][i][1], dp[u][i - j][0] + dp[v][j][1] + w);
dp[u][i][1] = min(dp[u][i][1], dp[u][i - j][1] + dp[v][j][0] + w * 2);
}
}
}
}
int main() {
scanf("%d%d%d", &n, &k, &x);
for (int i = 1; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].push_back({v, w});
g[v].push_back({u, w});
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j][0] = dp[i][j][1] = 0x3f3f3f3f;
}
}
dfs(x, -1);
printf("%d\n", min(dp[x][k][0], dp[x][k][1]));
}
Compilation message
museum.cpp: In function 'int main()':
museum.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d%d%d", &n, &k, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
museum.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d%d%d", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
261608 KB |
Output is correct |
2 |
Correct |
50 ms |
263764 KB |
Output is correct |
3 |
Correct |
86 ms |
265808 KB |
Output is correct |
4 |
Correct |
69 ms |
269396 KB |
Output is correct |
5 |
Correct |
60 ms |
273232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
261608 KB |
Output is correct |
2 |
Correct |
50 ms |
263764 KB |
Output is correct |
3 |
Correct |
86 ms |
265808 KB |
Output is correct |
4 |
Correct |
69 ms |
269396 KB |
Output is correct |
5 |
Correct |
60 ms |
273232 KB |
Output is correct |
6 |
Correct |
47 ms |
269396 KB |
Output is correct |
7 |
Correct |
83 ms |
271440 KB |
Output is correct |
8 |
Correct |
50 ms |
269396 KB |
Output is correct |
9 |
Correct |
48 ms |
269396 KB |
Output is correct |
10 |
Correct |
48 ms |
271440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
64 ms |
261608 KB |
Output is correct |
7 |
Correct |
50 ms |
263764 KB |
Output is correct |
8 |
Correct |
86 ms |
265808 KB |
Output is correct |
9 |
Correct |
69 ms |
269396 KB |
Output is correct |
10 |
Correct |
60 ms |
273232 KB |
Output is correct |
11 |
Correct |
47 ms |
269396 KB |
Output is correct |
12 |
Correct |
83 ms |
271440 KB |
Output is correct |
13 |
Correct |
50 ms |
269396 KB |
Output is correct |
14 |
Correct |
48 ms |
269396 KB |
Output is correct |
15 |
Correct |
48 ms |
271440 KB |
Output is correct |
16 |
Correct |
130 ms |
318804 KB |
Output is correct |
17 |
Correct |
703 ms |
539216 KB |
Output is correct |
18 |
Correct |
1876 ms |
318968 KB |
Output is correct |
19 |
Correct |
125 ms |
318756 KB |
Output is correct |
20 |
Correct |
108 ms |
320636 KB |
Output is correct |
21 |
Execution timed out |
3059 ms |
757212 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |