#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mxN = 1e4+5;
const ll INF = 1e18;
vector <pair <int, ll>> adj[mxN];
vector <ll> dp[mxN][2];
vector <ll> mg(vector <ll> x, vector <ll> y, ll k) {
vector <ll> z((int) x.size() + y.size() - 1, INF);
for (int i = 0; i < x.size(); i++) for (int j = 0; j < y.size(); j++) z[i+j] = min(z[i+j], x[i] + y[j] + k*(j > 0));
return z;
}
void dfs(int u, int p) {
dp[u][0] = dp[u][1] = {0, 0};
for (auto [v, w] : adj[u]) if (v != p) {
dfs(v, u);
dp[u][0] = mg(dp[u][0], dp[v][1], 2*w);
dp[u][0] = mg(dp[u][1], dp[v][0], w);
dp[u][1] = mg(dp[u][1], dp[v][1], 2*w);
}
for (int i = 0; i < dp[u][0].size(); i++) dp[u][0][i] = min(dp[u][0][i], dp[u][1][i]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, k, x;
cin >> n >> k >> x;
for (int i = 1; i < n; i++) {
int u, v; ll w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
dfs(x, x);
cout << dp[x][0][k] << '\n';
}
| # | 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... |