#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
const int N = 2e3 + 5;
const ll oo = 3e8;
const ll mod = 1e9 + 7; // 998244353;
void minz (int &u, int v){
if (u > v)
u = v;
}
vector <int> dp[N][2];
vector <pii> a[N];
void dfs (int u, int p){
dp[u][0].pb(0);
dp[u][1].pb(0);
for (pii t : a[u]){
int v = t.st, w = t.nd;
if (v == p) continue;
dfs(v, u);
int lmt = dp[u][0].size();
forf (i, 0, dp[v][0].size()){
dp[u][0].pb(oo);
dp[u][1].pb(oo);
}
ford (i, lmt - 1, 0){
forf (j, 0, dp[v][0].size()){
// cout << i << " " << j << " " << i + j + 1 << "\n";
minz(dp[u][0][i + j + 1], dp[u][0][i] + dp[v][0][j] + 2 * w);
minz(dp[u][1][i + j + 1], dp[u][1][i] + dp[v][0][j] + 2 * w);
minz(dp[u][1][i + j + 1], dp[u][0][i] + dp[v][1][j] + w);
}
}
}
}
int n, k, root, u, v, w;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef hqm
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n >> k >> root;
forf (i, 1, n){
cin >> u >> v >> w;
a[u].pb({v, w});
a[v].pb({u, w});
}
dfs(root, root);
cout << dp[root][1][k - 1];
return 0;
}
/*
*/
Compilation message
museum.cpp: In function 'void dfs(int, int)':
museum.cpp:11:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
| ^
museum.cpp:44:3: note: in expansion of macro 'forf'
44 | forf (i, 0, dp[v][0].size()){
| ^~~~
museum.cpp:11:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
| ^
museum.cpp:50:4: note: in expansion of macro 'forf'
50 | forf (j, 0, dp[v][0].size()){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |