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 <bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l); i<=(r); ++i)
#define REP(i,l,r) for(int i=(l); i<(r); ++i)
#define FORD(i,r,l) for(int i=(r); i>=(l); --i)
#define REPD(i,r,l) for(int i=(r)-1; i>=(l); --i)
#define pii pair<int, int>
using namespace std;
const int N = 1e5 + 5, Ns4 = 2005;
int n;
long long ans[N];
long long dpx[N], val[N];
int sz[Ns4];
long long dp[Ns4][Ns4], dd[Ns4][2];
vector<pii> S[N];
void sub1() {
sort(S[0].begin(), S[0].end());
REP(i, 0, n - 1) ans[n - i - 2] = ans[n - i - 1] + S[0][i].first;
}
void sub2() {
REP(i, 0, n - 1) {
for(auto [c, v] : S[i]) if (i + 1 == c)
val[i + 1] = c;
}
dpx[1] = val[1];
REP(i, 1, n) ans[0] += val[i];
REP(i, 2, n) {
dpx[i] = min(dpx[i - 1], dpx[i - 2]) + val[i];
}
ans[1] = min(dpx[n - 1], dpx[n - 2]);
}
void DFS(int u, int p, int max_sz) {
fill(dp[u] + 1, dp[u] + sz[u] + 1, 1e15);
dp[u][0] = 0;
int now_sz = min(sz[u], max_sz);
for(auto [c, v] : S[u]) if (v != p) {
DFS(v, u, max_sz);
FORD(i, now_sz, 1) {
dp[u][i] = min(dp[u][i] + dd[v][1] + c, dp[u][i - 1] + dd[v][0]);
}
dp[u][0] += dd[v][1] + c;
}
if (now_sz < max_sz) ++now_sz;
if (now_sz) dd[u][0] = dp[u][now_sz - 1]; else dd[u][0] = 1e15;
dd[u][1] = dp[u][now_sz];
}
void sub34() {
REP(i, 0, n) {
sz[i] = S[i].size() - 1;
if (i == 0) ++sz[i];
}
REP(i, 0, n) {
DFS(0, 0, i);
ans[i] = min(dd[0][1], dd[0][0]);
}
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
REP(i, 0, n - 1) {
S[U[i]].emplace_back(W[i], V[i]);
S[V[i]].emplace_back(W[i], U[i]);
}
if (n <= 2000) sub34();
else if (S[0].size() == n - 1) sub1();
else sub2();
vector<long long> ret(n);
REP(i, 0, n) ret[i] = ans[i];
return ret;
}
Compilation message (stderr)
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:68:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | else if (S[0].size() == n - 1) sub1();
| ~~~~~~~~~~~~^~~~~~~~
# | 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... |