This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 10000 + 2;
const int inf = 1 << 28;
int sz[N];
vector<pair<int, int>> g[N];
int dp[N][2][N];
void calc_dp(int u, int p) {
sz[u] = 1;
for (auto [v, c]: g[u]) {
if (v == p) continue;
calc_dp(v, u);
sz[u] += sz[v];
}
for (int i = 0; i <= sz[u]; i++) dp[u][0][i] = dp[u][1][i] = inf;
int len = 1;
dp[u][0][0] = dp[u][1][0] = 0;
dp[u][0][1] = dp[u][1][1] = 0;
for (auto [v, c]: g[u]) {
if (v == p) continue;
len += sz[v];
for (int i = len; i >= 0; i--) {
for (int j = 0; j <= min(i, sz[v]); j++) {
dp[u][0][i] = min(dp[u][0][i], dp[u][1][i - j] + dp[v][0][j] + c);
dp[u][0][i] = min(dp[u][0][i], dp[u][0][i - j] + dp[v][1][j] + c + c);
dp[u][1][i] = min(dp[u][1][i], dp[u][1][i - j] + dp[v][1][j] + c + c);
}
}
}
}
void solve() {
int n, k, x;
cin >> n >> k >> x;
for (int i = 2; i <= n; i++) {
int u, v, c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
}
calc_dp(x, 0);
cout << dp[x][0][k] << '\n';
// cout << "Ei porjonto thik ache" << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
solve();
return 0;
}
# | 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... |