#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5;
struct edge{
int to,w;
};
int dp[N][N][2],son[N];
vector<edge>G[N];
int n,k,x;
inline void dfs(int u,int fa){
son[u]=1;
dp[u][1][1]=dp[u][1][0]=0;
for(auto v:G[u]){
if(v.to==fa) continue;
dfs(v.to,u);
for(int i=min(k,son[u]);i>=0;i--)
for(int j=min(k-i,son[v.to]);j>=0;j--){
dp[u][i+j][1]=min(dp[u][i+j][1],dp[u][i][1]+dp[v.to][j][1]+v.w*2);
dp[u][i+j][0]=min(dp[u][i+j][0],dp[u][i][1]+dp[v.to][j][0]+v.w);
dp[u][i+j][0]=min(dp[u][i+j][0],dp[u][i][0]+dp[v.to][j][1]+v.w*2);
}
son[u]+=son[v.to];
}
}
int main(){
// freopen("museum.in","r",stdin);
// freopen("museum.out","w",stdout);
memset(dp,0x3f,sizeof dp);
scanf("%d%d%d",&n,&k,&x);
for(int i=1;i<n;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
G[u].push_back({v,w});G[v].push_back({u,w});
}
dfs(x,0);
printf("%d",min(dp[x][k][1],dp[x][k][0]));
return 0;
}
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:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | int u,v,w;scanf("%d%d%d",&u,&v,&w);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
784212 KB |
Output is correct |
2 |
Correct |
222 ms |
784064 KB |
Output is correct |
3 |
Correct |
296 ms |
784024 KB |
Output is correct |
4 |
Correct |
214 ms |
784212 KB |
Output is correct |
5 |
Correct |
215 ms |
784212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
784572 KB |
Output is correct |
2 |
Correct |
222 ms |
784468 KB |
Output is correct |
3 |
Correct |
232 ms |
784864 KB |
Output is correct |
4 |
Correct |
219 ms |
784724 KB |
Output is correct |
5 |
Correct |
220 ms |
784572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
784572 KB |
Output is correct |
2 |
Correct |
222 ms |
784468 KB |
Output is correct |
3 |
Correct |
232 ms |
784864 KB |
Output is correct |
4 |
Correct |
219 ms |
784724 KB |
Output is correct |
5 |
Correct |
220 ms |
784572 KB |
Output is correct |
6 |
Correct |
226 ms |
784440 KB |
Output is correct |
7 |
Correct |
231 ms |
784724 KB |
Output is correct |
8 |
Correct |
225 ms |
784464 KB |
Output is correct |
9 |
Correct |
230 ms |
784680 KB |
Output is correct |
10 |
Correct |
223 ms |
784572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
784212 KB |
Output is correct |
2 |
Correct |
222 ms |
784064 KB |
Output is correct |
3 |
Correct |
296 ms |
784024 KB |
Output is correct |
4 |
Correct |
214 ms |
784212 KB |
Output is correct |
5 |
Correct |
215 ms |
784212 KB |
Output is correct |
6 |
Correct |
221 ms |
784572 KB |
Output is correct |
7 |
Correct |
222 ms |
784468 KB |
Output is correct |
8 |
Correct |
232 ms |
784864 KB |
Output is correct |
9 |
Correct |
219 ms |
784724 KB |
Output is correct |
10 |
Correct |
220 ms |
784572 KB |
Output is correct |
11 |
Correct |
226 ms |
784440 KB |
Output is correct |
12 |
Correct |
231 ms |
784724 KB |
Output is correct |
13 |
Correct |
225 ms |
784464 KB |
Output is correct |
14 |
Correct |
230 ms |
784680 KB |
Output is correct |
15 |
Correct |
223 ms |
784572 KB |
Output is correct |
16 |
Correct |
241 ms |
784464 KB |
Output is correct |
17 |
Correct |
294 ms |
784468 KB |
Output is correct |
18 |
Correct |
249 ms |
784724 KB |
Output is correct |
19 |
Correct |
296 ms |
784892 KB |
Output is correct |
20 |
Correct |
246 ms |
784592 KB |
Output is correct |
21 |
Correct |
357 ms |
784828 KB |
Output is correct |
22 |
Correct |
318 ms |
784592 KB |
Output is correct |
23 |
Correct |
535 ms |
784652 KB |
Output is correct |
24 |
Correct |
339 ms |
784684 KB |
Output is correct |
25 |
Correct |
388 ms |
784976 KB |
Output is correct |