This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,k,x,tim[10005],dp[10001][10001][2],son[10005];
struct l{
int tar,wei;
};
vector<l>q[10005];
void dfs(int now,int fa){
son[now]=1;
dp[now][0][0]=0;
dp[now][0][1]=0;
dp[now][1][1]=0;
dp[now][1][0]=0;
for(int i=0;i<q[now].size();i++){
if(q[now][i].tar==fa){
continue;
}
dfs(q[now][i].tar,now);
son[now]+=son[q[now][i].tar];
for(int j=min(k,son[now]);j>=2;j--){
for(int l=0;l<=min(j,son[q[now][i].tar]);l++){
dp[now][j][0]=min(dp[now][j][0],dp[now][j-l][0]+dp[q[now][i].tar][l][0]+2*q[now][i].wei);
dp[now][j][1]=min(dp[now][j][1],dp[now][j-l][0]+dp[q[now][i].tar][l][1]+q[now][i].wei);
dp[now][j][1]=min(dp[now][j][1],dp[now][j-l][1]+dp[q[now][i].tar][l][0]+2*q[now][i].wei);
}
// cout<<now<<" "<<j<<" "<<dp[now][j][0]<<" "<<dp[now][j][1]<<endl;
}
}
}
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);
q[u].push_back({v,w});
q[v].push_back({u,w});
}
dfs(x,0);
printf("%d",min(dp[x][k][0],dp[x][k][1]));
return 0;
}
Compilation message (stderr)
museum.cpp: In function 'void dfs(int, int)':
museum.cpp:14:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<l>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i=0;i<q[now].size();i++){
| ~^~~~~~~~~~~~~~
museum.cpp: In function 'int main()':
museum.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%d%d",&n,&k,&x);
| ~~~~~^~~~~~~~~~~~~~~~~~~
museum.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d%d",&u,&v,&w);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |