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 maxn = 1e4 + 5;
const int maxv = 1e4 + 5;
using ll = long long;
int dp[maxn][maxv][2];
int n, k;
vector<pair<int, int>> G[maxn];
int st;
int siz[maxn];
void dfs(int x, int fa)
{
siz[x] = 1;
for (auto p : G[x])
{
int y = p.first;
int w = p.second;
if (y == fa)
continue;
dfs(y, x);
siz[x] += siz[y];
for (int i = min(k, siz[x]); i >= 1; i--)
{
for (int j = 1; j <= min(i - 1, siz[y]); j++)
{
dp[x][i][0] = min(dp[x][i][0], dp[x][i - j - 1][0] + dp[y][j][0] + w * 2);
dp[x][i][1] = min(dp[x][i][1], dp[x][i - j - 1][1] + dp[y][j][0] + w * 2);
dp[x][i][1] = min(dp[x][i][1], dp[x][i - j - 1][0] + dp[y][j][1] + w);
}
dp[x][i][1] = min(dp[x][i][1], dp[x][0][0] + dp[y][i][1] + w);
}
}
}
int main()
{
// freopen("museum.in", "r", stdin);
// freopen("museum.out", "w", stdout);
scanf("%d%d%d", &n, &k, &st);
for (int i = 1; i <= n - 1; i++)
{
int u, v;
int x;
scanf("%d%d%d", &u, &v, &x);
G[u].push_back(make_pair(v, x));
G[v].push_back(make_pair(u, x));
}
if(k == 1){
printf("0\n");
return 0;
}
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i <= n; i++)
{
dp[i][0][0] = dp[i][0][1] = 0;
}
dfs(st, 0);
printf("%d\n", dp[st][k - 1][1]);;;;;;
return 0;
}
Compilation message (stderr)
museum.cpp: In function 'int main()':
museum.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d%d%d", &n, &k, &st);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d%d%d", &u, &v, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |