#include<bits/stdc++.h>
using namespace std;
int const maxn = 1e4 + 5;
vector < pair < int, int > > g[maxn];
int dp[maxn][maxn][2], inf = 1e9 + 7, n, sz[maxn];
int f[maxn][2];
void dfs(int v, int p) {
sz[v] = 1;
for (int i = 1; i <= n; i++) {
dp[v][i][0] = dp[v][i][1] = inf;
}
dp[v][1][0] = dp[v][1][1] = 0;
for (auto [u, w] : g[v]) {
if (u == p) continue;
dfs(u, v);
for (int i = 1; i <= sz[v] + sz[u]; i++) f[i][0] = f[i][1] = inf;
for (int j = 1; j <= sz[v]; j++) {
for (int jx = 0; jx < 2; jx++) {
for (int z = 1; z <= sz[u]; z++) {
for (int jz = 0; jz < 2 - jx; jz++) {
f[j + z][jx|jz] = min(f[j + z][jx|jz], dp[v][j][jx] + dp[u][z][jz] + w * (2 - jz));
}
}
}
}
sz[v] += sz[u];
for (int i = 1; i <= sz[v]; i++) {
dp[v][i][0] = min(dp[v][i][0], f[i][0]);
dp[v][i][1] = min(dp[v][i][1], f[i][1]);
}
}
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k, x;
cin >> n >> k >> x;
for (int i = 1; i < n; i++) {
int u, v, c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
}
dfs(x, 0);
cout << dp[x][k][1] << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
museum.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
37 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |