#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 = 1e4 + 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, 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][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()){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
4152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
4152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |