#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#pragma "Ofast"
const int maxn = 1e4+10, inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
int n, k, x, siz[maxn], dp[maxn][maxn][2];
vector<pii> grafo[maxn];
inline void dfs(const int &u, const int &p = 0){
siz[u] = 1;
for(auto v : grafo[u]){
if(v.f == p) continue;
dfs(v.f, u);
siz[u] += siz[v.f];
}
}
int solve(const int &u, const int &p = 0){
int sizes = 1, d;
for(auto v : grafo[u]){
if(v.f == p) continue;
solve(v.f, u);
sizes += siz[v.f];
d = v.s;
for(int i=sizes; i>=2; i--){
for(int j=max(0, i-sizes+siz[v.f]); j<=min(i, siz[v.f]); j++){
dp[u][i][0] = min({dp[u][i][0], dp[u][i-j][0] + dp[v.f][j][1]
+ 2*d, dp[u][i-j][1] + dp[v.f][j][0] + d});
dp[u][i][1] = min({dp[u][i][1], dp[u][i-j][1] + dp[v.f][j][1] + 2*d});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin >> n >> k >> x;
for(int i=1; i<n; i++){
int u, v, d;
cin >> u >> v >> d;
grafo[u].push_back({v, d});
grafo[v].push_back({u, d});
}
dfs(x);
for(int i=1; i<=n; i++){
for(int j=2; j<=siz[i]; j++){
dp[i][j][0] = dp[i][j][1] = inf;
}
}
solve(x);
cout << dp[x][k][0] << endl;
return 0;
}
Compilation message
museum.cpp:8:0: warning: ignoring #pragma "Ofast" [-Wunknown-pragmas]
#pragma "Ofast"
museum.cpp: In function 'int solve(const int&, const int&)':
museum.cpp:50:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
558 ms |
28920 KB |
Output is correct |
2 |
Correct |
560 ms |
29056 KB |
Output is correct |
3 |
Correct |
852 ms |
165240 KB |
Output is correct |
4 |
Correct |
667 ms |
73080 KB |
Output is correct |
5 |
Correct |
577 ms |
40312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
558 ms |
28920 KB |
Output is correct |
2 |
Correct |
560 ms |
29056 KB |
Output is correct |
3 |
Correct |
852 ms |
165240 KB |
Output is correct |
4 |
Correct |
667 ms |
73080 KB |
Output is correct |
5 |
Correct |
577 ms |
40312 KB |
Output is correct |
6 |
Correct |
561 ms |
23424 KB |
Output is correct |
7 |
Correct |
738 ms |
107768 KB |
Output is correct |
8 |
Correct |
1089 ms |
2688 KB |
Output is correct |
9 |
Correct |
886 ms |
3064 KB |
Output is correct |
10 |
Correct |
591 ms |
3876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
558 ms |
28920 KB |
Output is correct |
7 |
Correct |
560 ms |
29056 KB |
Output is correct |
8 |
Correct |
852 ms |
165240 KB |
Output is correct |
9 |
Correct |
667 ms |
73080 KB |
Output is correct |
10 |
Correct |
577 ms |
40312 KB |
Output is correct |
11 |
Correct |
561 ms |
23424 KB |
Output is correct |
12 |
Correct |
738 ms |
107768 KB |
Output is correct |
13 |
Correct |
1089 ms |
2688 KB |
Output is correct |
14 |
Correct |
886 ms |
3064 KB |
Output is correct |
15 |
Correct |
591 ms |
3876 KB |
Output is correct |
16 |
Correct |
558 ms |
29668 KB |
Output is correct |
17 |
Correct |
556 ms |
29184 KB |
Output is correct |
18 |
Correct |
682 ms |
82680 KB |
Output is correct |
19 |
Correct |
928 ms |
2812 KB |
Output is correct |
20 |
Correct |
572 ms |
4856 KB |
Output is correct |
21 |
Correct |
720 ms |
106104 KB |
Output is correct |
22 |
Correct |
545 ms |
29176 KB |
Output is correct |
23 |
Correct |
929 ms |
2936 KB |
Output is correct |
24 |
Correct |
559 ms |
4728 KB |
Output is correct |
25 |
Correct |
873 ms |
169980 KB |
Output is correct |