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;
const int dydis = 2001;
vector<pair<int, int> > gr[dydis];
const long long inf = 1e14 + 1;
int n, m;
vector<int> mas;
auto lin() {
/// Galiu praleisti max 1 is eiles. Kokia min suma?
long long dp[n]; /// dp[i] - turiu visus iki i ir i-aji emiau. Kokia min suma?
dp[0] = mas[0];
dp[1] = mas[1];
for(int i = 2; i < n; i++) {
dp[i] = min(dp[i-1] + mas[i], dp[i-2] + mas[i]);
}
vector<long long> ret(n, 0);
for(auto x : mas) ret[0] += x;
ret[1] = min(dp[n-1], dp[n-2]);
return ret;
}
auto starr() {
//cout << "star - " << true << endl;
sort(mas.begin(), mas.end());
reverse(mas.begin(), mas.end());
vector<long long> ret(n, 0);
for(auto x : mas) ret[0] += x;
long long sum = 0;
for(int i = n-1; i > 0; i--) {
if(i != n- 1) sum += mas[i];
ret[i] = sum;
}
return ret;
}
long long dp[dydis][dydis][2]; /// dp[v][k][isimuVirsu]
void dfs(int v, int came, int wUp, int k) {
vector<long long> pos;
for(auto x : gr[v]) {
if(x.first == came) continue;
dfs(x.first, v, x.second, k);
pos.push_back(dp[x.first][k][1]);
}
sort(pos.begin(), pos.end());
int reikIsimt = 0;
if(k == 0) { /// turiu paimti viska!!!
long long sm = wUp;
for(auto x : gr[v]) {
if(x.first == came) continue;
sm += dp[x.first][k][1];
}
dp[v][k][1] = sm;
dp[v][k][0] = inf;
if(v == 0) dp[v][k][0] = sm;
return ;
}
/// dp[v][k][0] - jei neimu virsaus
reikIsimt = max(0, (int)gr[v].size() - k);
long long sum = 0;
vector<long long> vec;
for(auto x : gr[v]) {
if(x.first == came) continue;
sum += dp[x.first][k][0]; /// pridedu kas bus jei neerasinsiu
vec.push_back(-dp[x.first][k][0] + dp[x.first][k][1]);
}
sort(vec.begin(), vec.end());
dp[v][k][0] = sum;
for(int i = 0; i < reikIsimt; i++) {
dp[v][k][0] += vec[i];
}
dp[v][k][1] = sum + wUp;
reikIsimt = max(0, (int)gr[v].size() - k - 1);
for(int i = 0; i < reikIsimt; i++) {
dp[v][k][1] += vec[i];
}
}
vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
n = N; m = W.size();
bool line = true, star = true;
for(int i = 0; i < m; i++) {
int a = U[i]; int b = V[i]; int w = W[i];
if(a != b-1) line = false;
if(a != 0) star = false;
gr[a].push_back({b, w});
gr[b].push_back({a, w});
mas.push_back(w);
}
if(line) {
return lin();
}
if(star) {
return starr();
}
vector<long long> ret(n, 0);
for(int i = 0; i < n; i++) {
dfs(0, 0, 0, i);
ret[i] = dp[0][i][0];
}
return ret;
}
# | 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... |