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;
#define int int64_t
#define pb push_back
const int lim=10100;
const int mod=1e9+7;
using pii=pair<int,int>;
vector<pii>v[lim];
int sz[lim];
int dp[2][lim][lim];
void dfs(int node,int par){
sz[node]=1;
for(int i=0;i<lim;i++){
dp[0][node][i]=dp[1][node][i]=INT_MAX;
}
dp[0][node][1]=dp[1][node][1]=0;
for(auto[child,c]:v[node]){
if(child==par)continue;
dfs(child,node);
for(int i=sz[node];i;i--){
for(int j=1;j<=sz[child];j++){
dp[0][node][i+j]=min(dp[0][node][i+j],dp[0][node][i]+dp[0][child][j]+2*c);
dp[1][node][i+j]=min(
{
dp[1][node][i+j],
dp[0][node][i]+dp[1][child][j]+c,
dp[1][node][i]+dp[0][child][j]+2*c
}
);
}
}
sz[node]+=sz[child];
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);freopen(".out","w",stdout);
#endif
int n,k,x;
cin>>n>>k>>x;
for(int i=0;i<n-1;i++){
int X,Y,Z;
cin>>X>>Y>>Z;
v[X].pb({Y,Z});
v[Y].pb({X,Z});
}
dfs(x,0);
cout<<dp[1][x][k]<<"\n";
}
# | 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... |