#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e4+5;
int n,k,x,inu,inv,inw,dp[MAXN][MAXN][2],siz[MAXN],minn=INT_MAX;
vector <pair<int,int> > lis[MAXN];
void dfs(int u,int fa){
dp[u][1][0]=0;
dp[u][1][1]=0;
siz[u]=1;
for(auto it:lis[u]){
int v=it.first;
int w=it.second;
if(v==fa) continue;
dfs(v,u);
siz[u]+=siz[v];
for(int i=min(siz[u],k);i>=1;i--)
for(int j=1;j<=min(siz[v],i-1);j++){
//if(i-j-2){
dp[u][i][0]=min(dp[u][i][0],dp[u][i-j][0]+dp[v][j][0]+w*2);
dp[u][i][1]=min(dp[u][i][1],dp[u][i-j][1]+dp[v][j][0]+w*2);
dp[u][i][1]=min(dp[u][i][1],dp[u][i-j][0]+dp[v][j][1]+w);
}
}
}
int main(){
// freopen("museum.in","r",stdin);
// freopen("museum.out","w",stdout);
scanf("%d%d%d",&n,&k,&x);
for(int i=1;i<n;i++){
scanf("%d%d%d",&inu,&inv,&inw);
lis[inu].push_back({inv,inw});
lis[inv].push_back({inu,inw});
}
memset(dp,0x3f,sizeof dp);
dfs(x,-1);
/* for(int i=1;i<=n;i++){
printf("I%d\n",i);
for(int j=1;j<=k;j++) printf("%d %d ",dp[i][j][0],dp[i][j][1]);
printf("\n");
}*/
printf("%d",min(dp[x][k][0],dp[x][k][1]));
}
Compilation message
museum.cpp: In function 'int main()':
museum.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d%d%d",&n,&k,&x);
| ~~~~~^~~~~~~~~~~~~~~~~~~
museum.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d%d%d",&inu,&inv,&inw);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
784208 KB |
Output is correct |
2 |
Correct |
286 ms |
784176 KB |
Output is correct |
3 |
Correct |
223 ms |
784108 KB |
Output is correct |
4 |
Correct |
227 ms |
784448 KB |
Output is correct |
5 |
Correct |
224 ms |
784208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
784724 KB |
Output is correct |
2 |
Correct |
232 ms |
784720 KB |
Output is correct |
3 |
Correct |
272 ms |
784976 KB |
Output is correct |
4 |
Correct |
264 ms |
784972 KB |
Output is correct |
5 |
Correct |
243 ms |
784596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
784724 KB |
Output is correct |
2 |
Correct |
232 ms |
784720 KB |
Output is correct |
3 |
Correct |
272 ms |
784976 KB |
Output is correct |
4 |
Correct |
264 ms |
784972 KB |
Output is correct |
5 |
Correct |
243 ms |
784596 KB |
Output is correct |
6 |
Correct |
263 ms |
784528 KB |
Output is correct |
7 |
Correct |
268 ms |
784884 KB |
Output is correct |
8 |
Correct |
256 ms |
784648 KB |
Output is correct |
9 |
Correct |
246 ms |
784548 KB |
Output is correct |
10 |
Correct |
284 ms |
784724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
784208 KB |
Output is correct |
2 |
Correct |
286 ms |
784176 KB |
Output is correct |
3 |
Correct |
223 ms |
784108 KB |
Output is correct |
4 |
Correct |
227 ms |
784448 KB |
Output is correct |
5 |
Correct |
224 ms |
784208 KB |
Output is correct |
6 |
Correct |
233 ms |
784724 KB |
Output is correct |
7 |
Correct |
232 ms |
784720 KB |
Output is correct |
8 |
Correct |
272 ms |
784976 KB |
Output is correct |
9 |
Correct |
264 ms |
784972 KB |
Output is correct |
10 |
Correct |
243 ms |
784596 KB |
Output is correct |
11 |
Correct |
263 ms |
784528 KB |
Output is correct |
12 |
Correct |
268 ms |
784884 KB |
Output is correct |
13 |
Correct |
256 ms |
784648 KB |
Output is correct |
14 |
Correct |
246 ms |
784548 KB |
Output is correct |
15 |
Correct |
284 ms |
784724 KB |
Output is correct |
16 |
Correct |
295 ms |
784792 KB |
Output is correct |
17 |
Correct |
778 ms |
784792 KB |
Output is correct |
18 |
Correct |
2061 ms |
784908 KB |
Output is correct |
19 |
Correct |
270 ms |
784784 KB |
Output is correct |
20 |
Correct |
302 ms |
784832 KB |
Output is correct |
21 |
Execution timed out |
3091 ms |
784648 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |