#include "bits/stdc++.h"
using namespace std;
#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]=1e9+1;
}
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
471 ms |
791804 KB |
Output is correct |
2 |
Correct |
488 ms |
791896 KB |
Output is correct |
3 |
Correct |
490 ms |
792276 KB |
Output is correct |
4 |
Correct |
510 ms |
791892 KB |
Output is correct |
5 |
Correct |
490 ms |
791876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
471 ms |
791804 KB |
Output is correct |
2 |
Correct |
488 ms |
791896 KB |
Output is correct |
3 |
Correct |
490 ms |
792276 KB |
Output is correct |
4 |
Correct |
510 ms |
791892 KB |
Output is correct |
5 |
Correct |
490 ms |
791876 KB |
Output is correct |
6 |
Correct |
494 ms |
791856 KB |
Output is correct |
7 |
Correct |
523 ms |
791900 KB |
Output is correct |
8 |
Correct |
486 ms |
791848 KB |
Output is correct |
9 |
Correct |
464 ms |
791880 KB |
Output is correct |
10 |
Correct |
454 ms |
791688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
471 ms |
791804 KB |
Output is correct |
7 |
Correct |
488 ms |
791896 KB |
Output is correct |
8 |
Correct |
490 ms |
792276 KB |
Output is correct |
9 |
Correct |
510 ms |
791892 KB |
Output is correct |
10 |
Correct |
490 ms |
791876 KB |
Output is correct |
11 |
Correct |
494 ms |
791856 KB |
Output is correct |
12 |
Correct |
523 ms |
791900 KB |
Output is correct |
13 |
Correct |
486 ms |
791848 KB |
Output is correct |
14 |
Correct |
464 ms |
791880 KB |
Output is correct |
15 |
Correct |
454 ms |
791688 KB |
Output is correct |
16 |
Correct |
503 ms |
791824 KB |
Output is correct |
17 |
Correct |
463 ms |
791960 KB |
Output is correct |
18 |
Correct |
489 ms |
791892 KB |
Output is correct |
19 |
Correct |
484 ms |
791780 KB |
Output is correct |
20 |
Correct |
468 ms |
791888 KB |
Output is correct |
21 |
Correct |
458 ms |
791972 KB |
Output is correct |
22 |
Correct |
458 ms |
791976 KB |
Output is correct |
23 |
Correct |
497 ms |
791640 KB |
Output is correct |
24 |
Correct |
481 ms |
791924 KB |
Output is correct |
25 |
Correct |
499 ms |
792148 KB |
Output is correct |