// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1000 + 5;
const int inf = 1e9 + 7;
int n, k, r, dp[maxn][maxn][2], tmp[maxn][2], cnt[maxn];
vector<pii> adj[maxn];
void dfs(int v, int p){
cnt[v] = 1;
for(auto [u, w]: adj[v]){
if(u == p) continue;
dfs(u, v);
for(int i = 0; i <= cnt[v] + cnt[u]; i++) tmp[i][0] = tmp[i][1] = inf;
for(int i = 1; i <= cnt[v]; i++){
for(int j = 0; j <= cnt[u]; j++){
if(j == 0){
smin(tmp[i][1], dp[v][i][1]);
smin(tmp[i][0], dp[v][i][0]);
}
else{
smin(tmp[i + j][1], dp[v][i][1] + dp[u][j][1] + 2 * w);
int x = min(dp[v][i][1] + w + dp[u][j][0], dp[v][i][0] + 2 * w + dp[u][j][1]);
smin(tmp[i + j][0], x);
}
}
}
cnt[v] += cnt[u];
for(int i = 1; i <= cnt[v]; i++){
dp[v][i][0] = tmp[i][0];
dp[v][i][1] = tmp[i][1];
smin(dp[v][i][0], dp[v][i][1]);
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k >> r;
for(int i = 0; i < n - 1; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
dfs(r, -1);
cout << dp[r][k][0] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
1884 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
1884 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Runtime error |
4 ms |
1884 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |