#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <chrono>
#include <ctime>
#include <random>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <iterator>
#include <climits>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007
int N,K,X;
vector<pair<int,int>>adj[10001];
int subsize[10001];
vector<int>dp[10001][2]; //min cost , 0 is round trip , 1 is end
void dfs(int node, int par){
for (auto p:adj[node])
if (par!=p.first){
dfs(p.first,node);
subsize[node]+=subsize[p.first];
}
subsize[node]++;
for (int i=0;i<=subsize[node];i++){
dp[node][0].push_back(INF);
dp[node][1].push_back(INF);
}
dp[node][0][1]=0;
dp[node][1][1]=0;
for (auto p:adj[node])
if (par!=p.first){
for (int i=subsize[node]-subsize[p.first];i>=1;i--){
for (int j=1;j<=subsize[p.first] && i+j<=subsize[node];j++){
dp[node][0][i+j]=min(dp[node][0][i+j],dp[node][0][i]+dp[p.first][0][j]+p.second*2);
dp[node][1][i+j]=min(dp[node][1][i+j],dp[node][0][i]+dp[p.first][1][j]+p.second);
dp[node][1][i+j]=min(dp[node][1][i+j],dp[node][1][i]+dp[p.first][0][j]+p.second*2);
}
}
}
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);
//freopen (".in","r",stdin);
//freopen (".out","w",stdout);
cin>>N>>K>>X;
for (int i=1;i<N;i++){
int A,B,C;
cin>>A>>B>>C;
adj[A].push_back({B,C});
adj[B].push_back({A,C});
}
dfs(X,X);
cout<<dp[X][1][K]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
357 ms |
4384 KB |
Output is correct |
2 |
Correct |
356 ms |
4604 KB |
Output is correct |
3 |
Correct |
566 ms |
158200 KB |
Output is correct |
4 |
Correct |
429 ms |
55452 KB |
Output is correct |
5 |
Correct |
376 ms |
15572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
357 ms |
4384 KB |
Output is correct |
2 |
Correct |
356 ms |
4604 KB |
Output is correct |
3 |
Correct |
566 ms |
158200 KB |
Output is correct |
4 |
Correct |
429 ms |
55452 KB |
Output is correct |
5 |
Correct |
376 ms |
15572 KB |
Output is correct |
6 |
Correct |
357 ms |
3576 KB |
Output is correct |
7 |
Correct |
489 ms |
99420 KB |
Output is correct |
8 |
Correct |
472 ms |
2552 KB |
Output is correct |
9 |
Correct |
415 ms |
2604 KB |
Output is correct |
10 |
Correct |
363 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
357 ms |
4384 KB |
Output is correct |
7 |
Correct |
356 ms |
4604 KB |
Output is correct |
8 |
Correct |
566 ms |
158200 KB |
Output is correct |
9 |
Correct |
429 ms |
55452 KB |
Output is correct |
10 |
Correct |
376 ms |
15572 KB |
Output is correct |
11 |
Correct |
357 ms |
3576 KB |
Output is correct |
12 |
Correct |
489 ms |
99420 KB |
Output is correct |
13 |
Correct |
472 ms |
2552 KB |
Output is correct |
14 |
Correct |
415 ms |
2604 KB |
Output is correct |
15 |
Correct |
363 ms |
3448 KB |
Output is correct |
16 |
Correct |
358 ms |
5176 KB |
Output is correct |
17 |
Correct |
357 ms |
4856 KB |
Output is correct |
18 |
Correct |
443 ms |
65144 KB |
Output is correct |
19 |
Correct |
440 ms |
2552 KB |
Output is correct |
20 |
Correct |
359 ms |
3560 KB |
Output is correct |
21 |
Correct |
479 ms |
96504 KB |
Output is correct |
22 |
Correct |
356 ms |
4728 KB |
Output is correct |
23 |
Correct |
440 ms |
2552 KB |
Output is correct |
24 |
Correct |
357 ms |
3576 KB |
Output is correct |
25 |
Correct |
569 ms |
167704 KB |
Output is correct |