#include<bits/stdc++.h>
using namespace std;
const int N=10005;
int dp[N][N][2];
vector<pair<int,int> > g[N];
int siz[N];
int n,l,x;
void dfs(int x,int fa)
{
//cout<<x<<"\n";
dp[x][1][1]=dp[x][1][0]=0;
siz[x]=1;
if(g[x].size()==1&&g[x][0].first==fa)
{
return;
}
for(auto i:g[x])
{
int v=i.first;
int w=i.second;
if(v==fa) continue;
dfs(v,x);
for(int j=min(l,siz[x]);j>=0;j--)
{
for(int k=min(l-j,siz[v]);k>=0;k--)
{
dp[x][j+k][1]=min(dp[x][j+k][1],dp[x][j][1]+dp[v][k][1]+w*2);
dp[x][j+k][0]=min(dp[x][j+k][0],dp[x][j][1]+dp[v][k][0]+w);
dp[x][j+k][0]=min(dp[x][j+k][0],dp[x][j][0]+dp[v][k][1]+w*2);
}
}
siz[x]+=siz[v];
}
}
int main()
{
cin>>n>>l>>x;
for(int i=1;i<n;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=n;j++) dp[i][j][0]=dp[i][j][1]=1e9;
}
dfs(x,0);
cout<<min(dp[x][l][0],dp[x][l][1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
784212 KB |
Output is correct |
2 |
Correct |
274 ms |
784336 KB |
Output is correct |
3 |
Correct |
266 ms |
784468 KB |
Output is correct |
4 |
Correct |
268 ms |
784372 KB |
Output is correct |
5 |
Correct |
405 ms |
784344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
784212 KB |
Output is correct |
2 |
Correct |
274 ms |
784336 KB |
Output is correct |
3 |
Correct |
266 ms |
784468 KB |
Output is correct |
4 |
Correct |
268 ms |
784372 KB |
Output is correct |
5 |
Correct |
405 ms |
784344 KB |
Output is correct |
6 |
Correct |
270 ms |
784208 KB |
Output is correct |
7 |
Correct |
267 ms |
784460 KB |
Output is correct |
8 |
Correct |
278 ms |
784212 KB |
Output is correct |
9 |
Correct |
277 ms |
784424 KB |
Output is correct |
10 |
Correct |
272 ms |
784212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
409 ms |
784212 KB |
Output is correct |
7 |
Correct |
274 ms |
784336 KB |
Output is correct |
8 |
Correct |
266 ms |
784468 KB |
Output is correct |
9 |
Correct |
268 ms |
784372 KB |
Output is correct |
10 |
Correct |
405 ms |
784344 KB |
Output is correct |
11 |
Correct |
270 ms |
784208 KB |
Output is correct |
12 |
Correct |
267 ms |
784460 KB |
Output is correct |
13 |
Correct |
278 ms |
784212 KB |
Output is correct |
14 |
Correct |
277 ms |
784424 KB |
Output is correct |
15 |
Correct |
272 ms |
784212 KB |
Output is correct |
16 |
Correct |
285 ms |
784212 KB |
Output is correct |
17 |
Correct |
343 ms |
784328 KB |
Output is correct |
18 |
Correct |
307 ms |
784464 KB |
Output is correct |
19 |
Correct |
359 ms |
784212 KB |
Output is correct |
20 |
Correct |
294 ms |
784160 KB |
Output is correct |
21 |
Correct |
410 ms |
784632 KB |
Output is correct |
22 |
Correct |
366 ms |
784336 KB |
Output is correct |
23 |
Correct |
588 ms |
784248 KB |
Output is correct |
24 |
Correct |
393 ms |
784468 KB |
Output is correct |
25 |
Correct |
444 ms |
784468 KB |
Output is correct |