#include<bits/stdc++.h>
using namespace std;
#define N 10002
#define int long long
#define oo 1e18
#define fi first
#define se second
typedef pair<int, int> ii;
int dp[2][N][N], n, root, k, siz[N];
vector<ii> adj[N];
void dfs(int u, int p){
siz[u] = 1;
dp[1][1][u] = dp[0][1][u] = dp[0][0][u] = dp[1][0][u] = 0;
for (auto x : adj[u]){
int v = x.fi, w = x.se;
if (v == p) continue;
dfs(v, u);
for (int i = siz[u]; i >= 1; i--){
for (int j = 1; j <= siz[v]; j++){
dp[0][i + j][u] = min(dp[0][i + j][u], dp[1][i][u] + dp[0][j][v] + w);
dp[0][i + j][u] = min(dp[0][i + j][u], dp[0][i][u] + dp[1][j][v] + 2 * w);
dp[1][i + j][u] = min(dp[1][i + j][u], dp[1][i][u] + dp[1][j][v] + w * 2);
}
}
siz[u] += siz[v];
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> k >> root;
for (int i = 1; i <= n - 1; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int i = 1; i <= n; i++){
for (int j = 0; j <= n; j++){
dp[0][j][i] = dp[1][j][i] = oo;
}
}
dfs(root, 0);
cout << dp[0][k][root];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1008 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1008 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Runtime error |
1008 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |