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 <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define lli long long int
#define debugsl(a) cout << #a << " = " << a << ", "
#define debug(a) cout << #a << " = " << a << endl
#define MAX 2000
#define des first
#define w second
#define dif first.first
#define peso first.second
#define si second.first
#define talvez second.second
lli n,a,b,c;
lli dp[2][MAX+2];
vector<pair<lli,lli> > hijos[MAX+2];
void dfs(lli pos, lli padre, lli k) {
vector<pair<pair<lli,lli> ,pair<lli,lli> > > orden;
lli tam = 0;
for (auto h : hijos[pos]) {
if (h.des == padre) continue;
dfs(h.des,pos,k);
a = dp[0][h.des] + h.w;
b = dp[1][h.des];
c = b-a;
orden.push_back({{c,h.w},{a, b} });
tam++;
}
//sort de chico a grande
sort(orden.begin(), orden.end());
lli sum = 0;
lli cont = 0;
rep(i,0,k-1) {
if (i >= tam) break;
auto act = orden[i];
if (act.dif >= 0) break;
sum += act.talvez;
cont = i+1;
}
rep(i,cont,tam-1) {
a = orden[i].si;
b = orden[i].talvez + orden[i].peso;
sum += min(a,b);
}
dp[0][pos] = sum;
if (cont < k) dp[1][pos] = sum;
else {
sum -= orden[k-1].talvez;
a = orden[k-1].si;
b = orden[k-1].talvez + orden[k-1].peso;
sum += min(a,b);
dp[1][pos] = sum;
}
}
std::vector <long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
vector<long long> res (N, 0);
n = N;
//asigna hijos
rep(i,0,n-2) {
a = U[i]+1;
b = V[i]+1;
c = W[i];
hijos[a].push_back({b,c});
hijos[b].push_back({a,c});
res[0] += c;
}
//dp es [ cuantos me faltan para ya estar ] [ nodo ]
rep(i,1,n-1) {
rep(j,1,n) {dp[0][j] = 0; dp[1][j] = 0;}
dfs(1,0,i);
res[i] = dp[0][1];
}
return res;
}
# | 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... |