#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
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);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
335 ms |
784212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
225 ms |
784464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
225 ms |
784464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
335 ms |
784212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |