This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define MAXN 10005
#define ii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define sz(i) (int)i.size()
#define endl '\n'
using namespace std;
const int INF = 1e9;
int n, k, st;
vector<ii>g[MAXN];
namespace sub1
{
vector<int> dp[MAXN][2];
vector<int> combine(vector<int>x, vector<int>y,int w)
{
vector<int>ans(sz(x)+sz(y)-1,INF);
for(int i=1;i<sz(x);i++)
for(int j=0;j<sz(y);j++)
if(i+j<=k)
ans[i+j]=min(ans[i+j],x[i]+y[j]+w);
return ans;
}
vector<int>mn(vector<int>x,vector<int>y)
{
if(sz(x)<sz(y))
{
for(int i=0;i<sz(x);i++)
y[i]=min(x[i],y[i]);
return y;
}
for(int i=0;i<sz(y);i++)
x[i]=min(x[i],y[i]);
return x;
}
void dfs(int u, int p, int cost)
{
dp[u][0]={0,cost};
dp[u][1]={INF,cost};
for(ii i : g[u])
{
int v = i.F, w = i.S;
if(v == p)
continue;
dfs(v, u, w);
dp[u][1] = mn(dp[u][1],combine(dp[u][0], dp[v][1],0));
dp[u][0] = mn(dp[u][0],combine(dp[u][0], dp[v][0],w));
}
}
void solve()
{
dfs(st, 0, 0);
cout<<dp[st][1][k];
}
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> st;
for(int i = 1; i < n; i++)
{
int x, y, z;
cin >> x >> y >> z;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
sub1::solve();
}
Compilation message (stderr)
museum.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |