# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
969850 | europium | Road Closures (APIO21_roads) | C++17 | 0 ms | 0 KiB |
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 "roads.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void dfs(int node, int parent, int k, vector<vector<pair<int, int>>>& graph, vector<vector<ll>>& dp) {
dp[node][0] = 0;
dp[node][1] = 0;
for (auto& edge : graph[node]) {
int child = edge.first;
int cost = edge.second;
if (child == parent) {
continue;
}
dfs(child, node, k, graph, dp);
for (int i = k; i >= 0; --i) {
dp[node][i] += min(dp[child][min(i, k)] , dp[child][0]);
}
}
}
vector<ll> minimum_closure_costs(int N, vector<int>& U, vector<int>& V, vector<int>& W) {
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < N - 1; ++i) {
graph[U[i]].push_back({V[i], W[i]});
graph[V[i]].push_back({U[i], W[i]});
}
vector<vector<ll>> dp(N, vector<ll>(N + 1, numeric_limits<ll>::max()));
vector<ll> result;
for (int k = 0; k < N; ++k) {
dfs(0, -1, k, graph, dp);
result.push_back(dp[0][k]);
}
return result;
}