#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 10002
#define ALL(x) x.begin(), x.end()
int dp[N][N][2];
int n, k, x, sz[N];
vector<pair<int, int>> g[N];
/*
* at, take, comebacktonode
*
* dp[u][j][1] = min (t1 + t2 + ... + td == j - 1)
* ( 2w + dp[v][j][1] )
* dp[u][j][0] = min (t1 + t2 + ... + td == j - 1)
* (dp[v1][t1][1]+2*w1 + ... + dp[vd-1][td-1][1]+2*wd-1) + dp[vd][td][0] + wd
*
* ans = dp[x][k][0]
*/
void dfs(int u, int p)
{
sz[u] = 1;
dp[u][0][1] = dp[u][0][0] = dp[u][1][1] = dp[u][1][0] = 0;
for (auto [w, v] : g[u])
{
if (v == p) continue;
dfs(v, u);
for (int i = sz[u]; i >= 0; --i) for (int j = sz[v]; j >= 1; --j)
{
dp[u][i+j][1] = min(dp[u][i+j][1], 2*w + dp[v][j][1] + dp[u][i][1]);
dp[u][i+j][0] = min(dp[u][i+j][0], 2*w + dp[v][j][0] + dp[u][i][1]);
dp[u][i+j][0] = min(dp[u][i+j][0], w + dp[v][j][0] + dp[u][i][1]);
}
sz[u] += sz[v];
}
}
int main()
{
ShinLena;
cin >> n >> k >> x;
for (int i = 1, u, v, w; i < n; ++i)
cin >> u >> v >> w, g[u].emplace_back(w, v), g[v].emplace_back(w, u);
memset(dp, 63, sizeof dp);
dfs(x, x);
cout << dp[x][k][0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
783680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
317 ms |
784468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
317 ms |
784468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
783680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |