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
using namespace std;
const int INF = 1e9;
const int MAXN = 1e4;
vector<pair<int,int>> adj[MAXN+1];
int dp[MAXN+1][MAXN+1][2];
int st[MAXN+1];
void calcst(int u, int par){
st[u] = 1;
for(auto p : adj[u]){
if(p.f == par) continue;
calcst(p.f,u);
st[u] += st[p.f];
}
}
void dfs(int u,int par){
//dp[u][0][0] = dp[u][0][1] = dp[u][1][0] = dp[u][1][1] = 0;
for(int cur = 1; auto p : adj[u]){
if(p.f == par) continue;
dfs(p.f,u);
cur += st[p.f];
for(int i = cur;i>=2;--i){
for(int j = max(0,i-cur+st[p.f]);j<=min(i,st[p.f]);++j){
//cout << i-j << " " << j << " " << dp[u][i-j][0] << " " << dp[u][i-j][1] << " " << dp[p.f][j][0] << " " << dp[p.f][j][1] << "\n";
dp[u][i][0] = min(dp[u][i][0],dp[p.f][j][0]+dp[u][i-j][1]+p.s);
dp[u][i][0] = min(dp[u][i][0],dp[p.f][j][1]+dp[u][i-j][0]+2*p.s);
dp[u][i][1] = min(dp[u][i][1],dp[p.f][j][1]+dp[u][i-j][1]+2*p.s);
//cout << u << " " << i << " " << dp[u][i][0] << " " << dp[u][i][1] << "\n";
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,K,X; cin >> N >> K >> X;
for(int i = 0;i<=N-2;++i){
int a,b,c; cin >> a >> b >> c;
adj[a].push_back({b,c}); adj[b].push_back({a,c});
}
for(int i = 1;i<=N;++i){
for(int j = 2;j<=N;++j){
dp[i][j][0] = dp[i][j][1] = INF;
}
}
calcst(X,-1);
dfs(X,-1);
cout << dp[X][K][0] << "\n";
return 0;
}
Compilation message (stderr)
museum.cpp: In function 'void dfs(int, int)':
museum.cpp:21:22: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
21 | for(int cur = 1; auto p : adj[u]){
| ^~~~
# | 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... |