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;
using pii = pair<int, int>;
const int N = 1e4 + 5;
int n, k, c, x, y, z, sz[N], dp[N][2][N];
vector<pii> v[N];
void dfs (int cur, int par) {
sz[cur] = 1;
for (auto &[x, y]: v[cur]) {
if (par == x) continue;
dfs(x, cur);
for (int i = sz[cur]; ~i; i--) {
for (int j = sz[x]; ~j; j--) {
dp[cur][0][i + j] = min(dp[cur][0][i + j], dp[cur][1][i] + dp[x][0][j] + y);
dp[cur][0][i + j] = min(dp[cur][0][i + j], dp[cur][0][i] + dp[x][1][j] + y * 2);
dp[cur][1][i + j] = min(dp[cur][1][i + j], dp[cur][1][i] + dp[x][1][j] + y * 2);
}
}
sz[cur] += sz[x];
}
}
int main() {
scanf("%d %d %d", &n, &k, &c);
for (int i = 1; i < n; i++) {
scanf("%d %d %d", &x, &y, &z);
v[x].emplace_back(y, z); v[y].emplace_back(x, z);
}
for (int i = 1; i <= n; i++) {
for (auto j: {0, 1}) {
for (int k = 2; k <= n; k++) dp[i][j][k] = 1e9;
}
}
dfs(c, 0);
printf("%d", min(dp[c][0][k], dp[c][1][k]));
}
Compilation message (stderr)
museum.cpp: In function 'int main()':
museum.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d %d %d", &n, &k, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d %d %d", &x, &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |