#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iterator>
using namespace std;
#define ALL(c) (c).begin(),(c).end()
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
typedef pair<int,int> PII;
typedef long long int64;
#define M 10000
#define INF 1000000000
int N,K,X;
vector<PII> adj[M+1];
int sz[M+1];
int f[M+1][M+1][2];
int g[2][M+1][2];
void solve(int x, int p=-1) {
vector<PII> ch={{-1,-1}};
int c=0;
sz[x]=1;
for (PII yt : adj[x]) if (yt.first!=p) {
solve(yt.first, x);
sz[x]+=sz[yt.first];
ch.push_back(yt);
c++;
}
FOR (k,0,K) REP (t,2) f[x][k][t]=INF;
if (c==0) {
f[x][1][0]=0;
f[x][1][1]=0;
} else {
FOR (k,0,K) REP (t,2) { g[0][k][t]=INF; }
g[0][0][0]=0;
g[0][0][1]=0;
int s=0;
FOR (i,1,c) {
FOR (k,0,K) REP (t,2) { g[i%2][k][t]=INF; }
int si=sz[ch[i].first];
FOR (k,0,min(s,K)) {
// t=0
g[i%2][k][0]=min(g[i%2][k][0], g[(i-1)%2][k][0]);
FOR (h,1,min(si,K-k)) {
g[i%2][k+h][0]=min(g[i%2][k+h][0], g[(i-1)%2][k][0]+2*ch[i].second+f[ch[i].first][h][0]);
}
// t=1 (wildcard)
g[i%2][k][1]=min(g[i%2][k][1], g[(i-1)%2][k][1]);
FOR (h,1,min(si,K-k)) {
int score = min(g[(i-1)%2][k][1]+2*ch[i].second+f[ch[i].first][h][0], // don't use it
g[(i-1)%2][k][0]+1*ch[i].second+f[ch[i].first][h][1]); // use it
g[i%2][k+h][1]=min(g[i%2][k+h][1], score);
}
}
s+=si;
}
FOR (k,1,min(sz[x],K)) {
REP (t,2) {
f[x][k][t]=g[c%2][k-1][t];
}
}
}
}
int main() {
scanf("%d %d %d",&N,&K,&X);
REP (i,N-1) {
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
solve(X);
printf("%d\n",f[X][K][1]);
return 0;
}
Compilation message
museum.cpp: In function 'int main()':
museum.cpp:87:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&N,&K,&X);
^
museum.cpp:90:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&a,&b,&c);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
783852 KB |
Output is correct |
2 |
Correct |
0 ms |
783852 KB |
Output is correct |
3 |
Correct |
0 ms |
783852 KB |
Output is correct |
4 |
Correct |
0 ms |
783852 KB |
Output is correct |
5 |
Correct |
0 ms |
783852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
784248 KB |
Output is correct |
2 |
Correct |
16 ms |
784248 KB |
Output is correct |
3 |
Correct |
16 ms |
784796 KB |
Output is correct |
4 |
Correct |
19 ms |
784320 KB |
Output is correct |
5 |
Correct |
6 ms |
784248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
784248 KB |
Output is correct |
2 |
Correct |
16 ms |
784248 KB |
Output is correct |
3 |
Correct |
16 ms |
784796 KB |
Output is correct |
4 |
Correct |
19 ms |
784320 KB |
Output is correct |
5 |
Correct |
6 ms |
784248 KB |
Output is correct |
6 |
Correct |
19 ms |
784248 KB |
Output is correct |
7 |
Correct |
29 ms |
784500 KB |
Output is correct |
8 |
Correct |
29 ms |
784572 KB |
Output is correct |
9 |
Correct |
29 ms |
784388 KB |
Output is correct |
10 |
Correct |
13 ms |
784248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
783852 KB |
Output is correct |
2 |
Correct |
0 ms |
783852 KB |
Output is correct |
3 |
Correct |
0 ms |
783852 KB |
Output is correct |
4 |
Correct |
0 ms |
783852 KB |
Output is correct |
5 |
Correct |
0 ms |
783852 KB |
Output is correct |
6 |
Correct |
6 ms |
784248 KB |
Output is correct |
7 |
Correct |
16 ms |
784248 KB |
Output is correct |
8 |
Correct |
16 ms |
784796 KB |
Output is correct |
9 |
Correct |
19 ms |
784320 KB |
Output is correct |
10 |
Correct |
6 ms |
784248 KB |
Output is correct |
11 |
Correct |
19 ms |
784248 KB |
Output is correct |
12 |
Correct |
29 ms |
784500 KB |
Output is correct |
13 |
Correct |
29 ms |
784572 KB |
Output is correct |
14 |
Correct |
29 ms |
784388 KB |
Output is correct |
15 |
Correct |
13 ms |
784248 KB |
Output is correct |
16 |
Correct |
99 ms |
784248 KB |
Output is correct |
17 |
Correct |
379 ms |
784248 KB |
Output is correct |
18 |
Correct |
109 ms |
784364 KB |
Output is correct |
19 |
Correct |
109 ms |
784468 KB |
Output is correct |
20 |
Correct |
89 ms |
784248 KB |
Output is correct |
21 |
Correct |
616 ms |
784480 KB |
Output is correct |
22 |
Correct |
533 ms |
784248 KB |
Output is correct |
23 |
Correct |
589 ms |
784532 KB |
Output is correct |
24 |
Correct |
529 ms |
784248 KB |
Output is correct |
25 |
Correct |
649 ms |
784904 KB |
Output is correct |