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>
#include<fstream>
using namespace std;
ifstream fin("HCN.INP");
ofstream fout("HCN.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
//using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 1e6 + 5, mxq = 2e5 + 5, sq = 350, mxv = 2e6 + 5;
const ll inf = 1e9;
int n, k, root;
int dp[10001][10001], sz[10001], dp2[10001][10001];
vt<pii>adj[10001];
void ckmin(int &a, int b){
a = min(a, b);
}
void dfs(int s, int pre, int dep){
dp[s][1] = 0; dp2[s][1] = -dep;
sz[s] = 1;
for(auto [i, w]: adj[s]){
if(i != pre){
dfs(i, s, dep + w);
for(int j = sz[s] + sz[i]; j >= 1; j--){
for(int take = max(1, j - sz[s]); take <= min(j, sz[i]); take++){
ckmin(dp[s][j], dp[s][j - take] + dp[i][take] + 2 * w);
ckmin(dp2[s][j], dp2[s][j - take] + dp[i][take] + 2 * w);
ckmin(dp2[s][j], dp[s][j - take] + dp2[i][take] + 2 * w);
}
}
sz[s] += sz[i];
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k >> root;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= k; j++){
dp[i][j] = dp2[i][j] = inf;
}
}
for(int i = 1; i < n; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb({v, w}); adj[v].pb({u, w});
}
dfs(root, -1, 0);
cout << dp2[root][k];
return(0);
}
Compilation message (stderr)
museum.cpp: In function 'void dfs(int, int, int)':
museum.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [i, w]: adj[s]){
| ^
# | 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... |