#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
const int MXN = 1e4+1;
int n, k;
vector<vector<pair<int, ll>>> adj;
ll dp[MXN+1][MXN+1][2];
int sub[MXN+1];
const ll INF = 1e15;
void dfs(int p, int parent) {
int sz = 1;
for (auto i: adj[p]) if (i.first != parent) {
dfs(i.first, p);
sz += sub[i.first];
}
for_(i, 0, sz+1) dp[p][i][0] = dp[p][i][1] = INF;
dp[p][1][0] = dp[p][1][1] = 0;
for (auto i: adj[p]) if (i.first != parent) {
for (int ct = sz; ct >= 2; ct--) for (int t = min(ct-1, sub[i.first]); t >= 1; t--) {
dp[p][ct][1] = min(dp[p][ct][1], dp[p][ct-t][1] + dp[i.first][t][1] + 2*i.second);
dp[p][ct][0] = min(dp[p][ct][0], dp[p][ct-t][0] + dp[i.first][t][1] + 2*i.second);
dp[p][ct][0] = min(dp[p][ct][0], dp[p][ct-t][1] + dp[i.first][t][0] + i.second);
}
}
sub[p] = sz;
}
int main() {
#ifndef ONLINE_JUDGE
//freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int x; cin >> n >> k >> x;
adj.resize(n);
for_(i, 0, n-1) {
int a, b, w; cin >> a >> b >> w;
a -= 1; b -= 1;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
x -= 1;
dfs(x, x);
cout << dp[x][k][0] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1459 ms |
47828 KB |
Output is correct |
2 |
Correct |
2063 ms |
48424 KB |
Output is correct |
3 |
Execution timed out |
3078 ms |
44852 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1459 ms |
47828 KB |
Output is correct |
2 |
Correct |
2063 ms |
48424 KB |
Output is correct |
3 |
Execution timed out |
3078 ms |
44852 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1459 ms |
47828 KB |
Output is correct |
7 |
Correct |
2063 ms |
48424 KB |
Output is correct |
8 |
Execution timed out |
3078 ms |
44852 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |