#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e4 + 2 ;
int n , k , x ;
vector< vector< pair<int , int> > >adj(MAX) ;
int sz[MAX] ;
int dp[MAX][MAX][2] , tmp[MAX][2] ;
void dfs(int node , int par)
{
dp[node][1][0] = dp[node][1][1] = 0 ;
sz[node] = 1 ;
for(auto &childd : adj[node])
{
int child = childd.first , cost = childd.second ;
if(child == par)
continue ;
dfs(child , node) ;
for(int k1 = 0 ; k1 <= min(sz[node] , k) ; ++k1)
{
for(int k2 = 0 ; k2 <= sz[child] && k1 + k2 <= k ; ++k2)
{
tmp[k1+k2][0] = min(tmp[k1+k2][0] , dp[node][k1][0] + dp[child][k2][0] + (k2 > 0) * 2 * cost) ;
tmp[k1+k2][1] = min(tmp[k1+k2][1] , dp[node][k1][0] + dp[child][k2][1] + (k2 > 0) * cost) ;
tmp[k1+k2][1] = min(tmp[k1+k2][1] , dp[node][k1][1] + dp[child][k2][0] + (k2 > 0) * 2 * cost) ;
}
}
sz[node] += sz[child] ;
for(int i = 1 ; i <= min(sz[node] , k) ; ++i)
{
dp[node][i][0] = tmp[i][0] ;
dp[node][i][1] = tmp[i][1] ;
tmp[i][0] = tmp[i][1] = 1e9 ;
}
}
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>k>>x ;
for(int i = 0 ; i < n-1 ; ++i)
{
int x , y , z ;
cin>>x>>y>>z ;
adj[x].emplace_back(y , z) ;
adj[y].emplace_back(x , z) ;
}
for(int i = 0 ; i <= k ; ++i)
tmp[i][0] = tmp[i][1] = 1e9 ;
dfs(x , -1) ;
return cout<<dp[x][k][1]<<"\n" , 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
43264 KB |
Output is correct |
2 |
Correct |
34 ms |
43336 KB |
Output is correct |
3 |
Correct |
37 ms |
46072 KB |
Output is correct |
4 |
Correct |
35 ms |
44672 KB |
Output is correct |
5 |
Correct |
35 ms |
44156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
43264 KB |
Output is correct |
2 |
Correct |
34 ms |
43336 KB |
Output is correct |
3 |
Correct |
37 ms |
46072 KB |
Output is correct |
4 |
Correct |
35 ms |
44672 KB |
Output is correct |
5 |
Correct |
35 ms |
44156 KB |
Output is correct |
6 |
Correct |
42 ms |
43008 KB |
Output is correct |
7 |
Correct |
39 ms |
45696 KB |
Output is correct |
8 |
Correct |
37 ms |
42744 KB |
Output is correct |
9 |
Correct |
37 ms |
42752 KB |
Output is correct |
10 |
Correct |
38 ms |
42852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
36 ms |
43264 KB |
Output is correct |
7 |
Correct |
34 ms |
43336 KB |
Output is correct |
8 |
Correct |
37 ms |
46072 KB |
Output is correct |
9 |
Correct |
35 ms |
44672 KB |
Output is correct |
10 |
Correct |
35 ms |
44156 KB |
Output is correct |
11 |
Correct |
42 ms |
43008 KB |
Output is correct |
12 |
Correct |
39 ms |
45696 KB |
Output is correct |
13 |
Correct |
37 ms |
42744 KB |
Output is correct |
14 |
Correct |
37 ms |
42752 KB |
Output is correct |
15 |
Correct |
38 ms |
42852 KB |
Output is correct |
16 |
Correct |
64 ms |
43768 KB |
Output is correct |
17 |
Correct |
156 ms |
44360 KB |
Output is correct |
18 |
Correct |
100 ms |
57080 KB |
Output is correct |
19 |
Correct |
110 ms |
42744 KB |
Output is correct |
20 |
Correct |
87 ms |
43248 KB |
Output is correct |
21 |
Correct |
300 ms |
117880 KB |
Output is correct |
22 |
Correct |
209 ms |
44536 KB |
Output is correct |
23 |
Correct |
399 ms |
42848 KB |
Output is correct |
24 |
Correct |
224 ms |
43792 KB |
Output is correct |
25 |
Correct |
380 ms |
181032 KB |
Output is correct |