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>
using namespace std;
#define ll int
#define pll pair<int, int>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 1e4 + 2;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll block = 350;
ll n,k,x;
vector<pll>adj[N];
ll dp[N][N][2], sz[N], f[2][N][2];
void dfs(ll u, ll par){
for(auto v : adj[u]){
if(v.F == par) continue;
dfs(v.F, u);
}
for(int i = 0; i <= k;i++){
for(int j = 0; j <= 1;j++){
for(int l = 0; l <= 1;l++) f[j][i][l] = inf;
}
}
f[1][1][0] = 0;
sz[u] = 1;
for(int idx = 0; idx < adj[u].size();idx++){
ll v = adj[u][idx].F, w = adj[u][idx].S;
if(v == par){
for(int i = 0; i <= sz[u];i++){
for(int j = 0; j <= 1;j++) swap(f[0][i][j], f[1][i][j]);
}
continue;
}
for(int i = 0; i <= sz[u];i++){
for(int j = 0; j <= sz[v];j++){
f[idx & 1][i + j][0] = min(f[idx & 1][i + j][0], f[(idx & 1) ^ 1][i + j][0]);
f[idx & 1][i + j][0] = min(f[idx & 1][i + j][0], f[(idx & 1) ^ 1][i][0] + dp[v][j][0] + 2 * w);
f[idx & 1][i + j][1] = min(f[idx & 1][i + j][1], f[(idx & 1) ^ 1][i][0] + dp[v][j][1] + w);
f[idx & 1][i + j][1] = min(f[idx & 1][i + j][1], f[(idx & 1) ^ 1][i][1] + dp[v][j][0] + 2 * w);
f[idx & 1][i + j][1] = min(f[idx & 1][i + j][1], f[(idx & 1) ^ 1][i + j][1]);
}
}
sz[u] += sz[v];
}
for(int i = 0; i <= k;i++){
for(int j = 0; j <= 1;j++) dp[u][i][j] = min(f[0][i][j], f[1][i][j]);
}
dp[u][0][0] = 0, dp[u][1][0] = 0, dp[u][1][1] = 0, dp[u][0][1] = 0;
}
void to_thic_cau(){
cin >> n >> k >> x;
for(int i = 1; i <= n - 1;i++){
ll u,v,c; cin >> u >> v >> c;
adj[u].pb({v, c}); adj[v].pb({u, c});
}
dfs(x, 0);
cout << min(dp[x][k][0], dp[x][k][1]) << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
Compilation message (stderr)
museum.cpp: In function 'void dfs(int, int)':
museum.cpp:30:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int idx = 0; idx < adj[u].size();idx++){
| ~~~~^~~~~~~~~~~~~~~
# | 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... |