#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
const int nmax = 1e4 + 5;
const ll oo = 1e18, base = 311;
const int lg = 19, M = 10;
const ll mod = 1e9 + 7;
#define pii pair<ll, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;
int n, k, x;
int a[nmax];
vector<pii> adj[nmax];
int sz[nmax], f[nmax][nmax][2], tmp[nmax][2];
void ckmin(int &a, int b){
a = min(a, b);
}
void dfs(int u, int p){
sz[u] = 1;
f[u][1][0] = a[u] * 2;
f[u][1][1] = a[u];
for(auto [w, v] : adj[u]){
if(v == p) continue;
a[v] = w;
dfs(v, u);
for(int i = 0; i <= sz[u]; ++i){
tmp[i][0] = f[u][i][0];
tmp[i][1] = f[u][i][1];
}
for(int x = 0; x <= sz[u]; ++x){
for(int y = 0; y <= sz[v]; ++y){
if(x + y > k) continue;
ckmin(f[u][x + y][0], tmp[x][0] + f[v][y][0]);
ckmin(f[u][x + y][1], tmp[x][0] + f[v][y][1] - a[u]);
ckmin(f[u][x + y][1], tmp[x][1] + f[v][y][0]);
}
}
sz[u] += sz[v];
}
}
void update(){
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.ans", "w", stdout);
cin >> n >> k >> x;
// k = n - k;
for(int i = 1, u, v, w; i < n; ++i){
cin >> u >> v >> w;
adj[u].push_back({w, v});
adj[v].push_back({w, u});
}
memset(f, 0x3f, sizeof f);
dfs(x, -1);
cout << f[x][k][1];
}
/*
5 3 2
2 3 3
3 5 5
5 1 6
5 4 3
*/
Compilation message
museum.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
50 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
784192 KB |
Output is correct |
2 |
Correct |
153 ms |
784156 KB |
Output is correct |
3 |
Correct |
163 ms |
784024 KB |
Output is correct |
4 |
Correct |
152 ms |
784164 KB |
Output is correct |
5 |
Correct |
139 ms |
784088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
785064 KB |
Output is correct |
2 |
Correct |
161 ms |
785180 KB |
Output is correct |
3 |
Correct |
213 ms |
785628 KB |
Output is correct |
4 |
Correct |
172 ms |
785256 KB |
Output is correct |
5 |
Correct |
190 ms |
784996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
785064 KB |
Output is correct |
2 |
Correct |
161 ms |
785180 KB |
Output is correct |
3 |
Correct |
213 ms |
785628 KB |
Output is correct |
4 |
Correct |
172 ms |
785256 KB |
Output is correct |
5 |
Correct |
190 ms |
784996 KB |
Output is correct |
6 |
Correct |
181 ms |
784880 KB |
Output is correct |
7 |
Correct |
187 ms |
785416 KB |
Output is correct |
8 |
Correct |
291 ms |
784996 KB |
Output is correct |
9 |
Correct |
236 ms |
785004 KB |
Output is correct |
10 |
Correct |
174 ms |
785060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
784192 KB |
Output is correct |
2 |
Correct |
153 ms |
784156 KB |
Output is correct |
3 |
Correct |
163 ms |
784024 KB |
Output is correct |
4 |
Correct |
152 ms |
784164 KB |
Output is correct |
5 |
Correct |
139 ms |
784088 KB |
Output is correct |
6 |
Correct |
173 ms |
785064 KB |
Output is correct |
7 |
Correct |
161 ms |
785180 KB |
Output is correct |
8 |
Correct |
213 ms |
785628 KB |
Output is correct |
9 |
Correct |
172 ms |
785256 KB |
Output is correct |
10 |
Correct |
190 ms |
784996 KB |
Output is correct |
11 |
Correct |
181 ms |
784880 KB |
Output is correct |
12 |
Correct |
187 ms |
785416 KB |
Output is correct |
13 |
Correct |
291 ms |
784996 KB |
Output is correct |
14 |
Correct |
236 ms |
785004 KB |
Output is correct |
15 |
Correct |
174 ms |
785060 KB |
Output is correct |
16 |
Correct |
201 ms |
785088 KB |
Output is correct |
17 |
Correct |
217 ms |
784880 KB |
Output is correct |
18 |
Correct |
221 ms |
785240 KB |
Output is correct |
19 |
Correct |
248 ms |
784980 KB |
Output is correct |
20 |
Correct |
174 ms |
785040 KB |
Output is correct |
21 |
Correct |
229 ms |
785236 KB |
Output is correct |
22 |
Correct |
207 ms |
784964 KB |
Output is correct |
23 |
Correct |
303 ms |
784980 KB |
Output is correct |
24 |
Correct |
224 ms |
785300 KB |
Output is correct |
25 |
Correct |
249 ms |
785676 KB |
Output is correct |