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"
#ifndef EVAL
#include "grader.cpp"
#endif
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define ar array
#define ff first
#define ss second
const int N = 2e3 + 5;
vector<pair<int, ll>> edges[N];
ll dp[N][N][2];
void dfs(int u, int p = -1){
for(auto x : edges[u]){
if(x.ff == p) continue;
dfs(x.ff, u);
}
for(int t=0;t<2;t++){
for(int j=0;j<N;j++){
vector<ll> tt;
for(auto x : edges[u]){
if(x.ff == p) continue;
dp[u][j][t] += dp[x.ff][j][0] + x.ss;
if(j){
tt.push_back(dp[x.ff][j][1] - dp[x.ff][j][0] - x.ss);
}
}
sort(tt.begin(), tt.end());
for(int l=0;l<min((int)tt.size(), j - t);l++){
dp[u][j][t] += tt[l];
}
}
}
}
vector<ll> minimum_closure_costs(int n, vector<int> u, vector<int> v, vector<int> w) {
for(int i=0;i<n-1;i++){
edges[v[i]].push_back({u[i], w[i]});
edges[u[i]].push_back({v[i], w[i]});
}
dfs(0);
vector<ll> res;
for(int k=0;k<n;k++){
res.push_back(dp[0][k][0]);
} return res;
}
/*
5
0 1 1
0 2 4
0 3 3
2 4 2
*/
# | 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... |