#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] + 2 * cost) ;
tmp[k1+k2][1] = min(tmp[k1+k2][1] , dp[node][k1][0] + dp[child][k2][1] + cost) ;
tmp[k1+k2][1] = min(tmp[k1+k2][1] , dp[node][k1][1] + dp[child][k2][0] + 2 * cost) ;
}
}
sz[node] += sz[child] ;
for(int i = 1 ; i <= min(sz[node] , k) ; ++i)
{
dp[node][i][0] = min(dp[node][i][0] , tmp[i][0]) ;
dp[node][i][1] = min(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 = 1 ; i <= n ; ++i)
{
for(int j = 0 ; j <= k ; ++j)
dp[i][j][0] = dp[i][j][1] = tmp[j][0] = tmp[j][1] = 1e9 ;
}
dfs(x , -1) ;
return cout<<dp[x][k][1]<<"\n" , 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
50552 KB |
Output is correct |
2 |
Correct |
38 ms |
50560 KB |
Output is correct |
3 |
Correct |
41 ms |
50936 KB |
Output is correct |
4 |
Correct |
39 ms |
50432 KB |
Output is correct |
5 |
Correct |
39 ms |
50296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
50552 KB |
Output is correct |
2 |
Correct |
38 ms |
50560 KB |
Output is correct |
3 |
Correct |
41 ms |
50936 KB |
Output is correct |
4 |
Correct |
39 ms |
50432 KB |
Output is correct |
5 |
Correct |
39 ms |
50296 KB |
Output is correct |
6 |
Correct |
38 ms |
50432 KB |
Output is correct |
7 |
Correct |
40 ms |
50808 KB |
Output is correct |
8 |
Correct |
43 ms |
50552 KB |
Output is correct |
9 |
Correct |
43 ms |
50560 KB |
Output is correct |
10 |
Correct |
43 ms |
50560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
768 KB |
Output is correct |
6 |
Correct |
40 ms |
50552 KB |
Output is correct |
7 |
Correct |
38 ms |
50560 KB |
Output is correct |
8 |
Correct |
41 ms |
50936 KB |
Output is correct |
9 |
Correct |
39 ms |
50432 KB |
Output is correct |
10 |
Correct |
39 ms |
50296 KB |
Output is correct |
11 |
Correct |
38 ms |
50432 KB |
Output is correct |
12 |
Correct |
40 ms |
50808 KB |
Output is correct |
13 |
Correct |
43 ms |
50552 KB |
Output is correct |
14 |
Correct |
43 ms |
50560 KB |
Output is correct |
15 |
Correct |
43 ms |
50560 KB |
Output is correct |
16 |
Correct |
109 ms |
120824 KB |
Output is correct |
17 |
Correct |
398 ms |
433400 KB |
Output is correct |
18 |
Correct |
133 ms |
120952 KB |
Output is correct |
19 |
Correct |
159 ms |
120824 KB |
Output is correct |
20 |
Correct |
116 ms |
120824 KB |
Output is correct |
21 |
Correct |
716 ms |
746268 KB |
Output is correct |
22 |
Correct |
628 ms |
745848 KB |
Output is correct |
23 |
Correct |
821 ms |
745848 KB |
Output is correct |
24 |
Correct |
624 ms |
745868 KB |
Output is correct |
25 |
Correct |
722 ms |
746360 KB |
Output is correct |