//#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e17;
vector<vector<pair<int, ll>>> tree;
vector<vector<ll>> dp;
int n;
void dfs(int node, int par = 0) {
int ch = 0;
dp[node][0] = 0;
for (auto [next, w]: tree[node]) {
if (next == par) continue;
dfs(next, node);
dp[node][0] += w + dp[next][0];
}
for (int k = 1; k < n; k++) {
ll ans = 0;
vector<ll> ops;
for (auto [next, w]: tree[node]) {
if (next == par) continue;
ops.push_back(dp[next][k-1] - (dp[next][k] + w));
ans += dp[next][k] + w;
}
sort(ops.begin(), ops.end());
dp[node][k] = min(dp[node][k], ans);
for (int j = 0; j < min((int)ops.size(), k); j++) {
ans += ops[j];
dp[node][k] = min(dp[node][k], ans);
}
dp[node][k] = min(dp[node][k], dp[node][k-1]);
}
}
vector<ll> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
tree.clear();
tree.resize(n+1);
for (int i = 0; i < n-1; i++) {
U[i]++;
V[i]++;
tree[U[i]].push_back({V[i], W[i]});
tree[V[i]].push_back({U[i], W[i]});
}
dp.assign(n+1, vector<ll>(n+1, INF));
dfs(1);
vector<ll> ans;
for (int k = 0; k < n; k++) ans.push_back(dp[1][k]);
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |