#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define endl '\n'
int n,k,x;
vector<pair<int,int>> adj[10001];
int ss[10001];
int tt[10001];
void dfs(int no,int par2=-1){
tt[no]=0;
ss[no]=1;
for(auto j:adj[no]){
if(j.a==par2){
continue;
}
dfs(j.a,no);
tt[no]+=tt[j.a]+j.b;
ss[no]+=ss[j.a];
}
}
int dp[100001];
int tot=0;
int cot=0;
int ans=1e9;
int back[10001][10001];
void dfs2(int no,int par2=-1,int su=0){
cot+=1;
if(cot>=k){
ans=min(ans,(tot-dp[cot-k])*2-su);
}
for(int i=ss[no]-1;i<=n;i++){
if(dp[i-ss[no]+1]==-1){
back[no][i]=-1;
continue;
}
back[no][i]=max(dp[i],tt[no]+dp[i-ss[no]+1]);;
}
for(auto j:adj[no]){
if(j.a==par2){
continue;
}
tot+=j.b;
dfs2(j.a,no,su+j.b);
}
for(int i=n;i>=ss[no]-1;i--){
dp[i]=max(dp[i],back[no][i]);
/*if(dp[i-ss[no]+1]==-1){
continue;
}
dp[i]=max(dp[i],tt[no]+dp[i-ss[no]+1]);*/
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>k>>x;
for(int i=0;i<n-1;i++){
int aa,bb,cc;
cin>>aa>>bb>>cc;
aa--;
bb--;
adj[aa].pb({bb,cc});
adj[bb].pb({aa,cc});
}
for(int i=1;i<=n;i++){
dp[i]=-1;
}
dfs(x);
dfs2(x);
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
396 ms |
392184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
396 ms |
392184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |