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"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(7069);
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);
const int N = 2005;
ll dp[N][N][2];
vector<pair<int, ll>> g[N];
int n;
void dfs(int u = 1, int par = 1, ll len = 0) {
for (auto to : g[u]) {
if (to.ff == par) continue;
dfs(to.ff, u, to.ss);
}
for (int k = 1; k < n; ++k) {
int cur = (int)g[u].size();
multiset<ll> tarb;
for (auto to : g[u]) {
if (to.ff == par) continue;
if (dp[to.ff][k][1] <= dp[to.ff][k][0]) {
dp[u][k][0] += dp[to.ff][k][1];
--cur;
continue;
}
dp[u][k][0] += dp[to.ff][k][0];
tarb.insert(dp[to.ff][k][1] - dp[to.ff][k][0]);
}
while (cur > k) {
dp[u][k][0] += *tarb.begin();
tarb.erase(tarb.begin());
--cur;
}
if (u == 1) continue;
dp[u][k][1] = len;
cur = (int)g[u].size() - 1;
tarb.clear();
for (auto to : g[u]) {
if (to.ff == par) continue;
if (dp[to.ff][k][1] <= dp[to.ff][k][0]) {
dp[u][k][1] += dp[to.ff][k][1];
--cur;
continue;
}
dp[u][k][1] += dp[to.ff][k][0];
tarb.insert(dp[to.ff][k][1] - dp[to.ff][k][0]);
}
while (cur > k) {
dp[u][k][1] += *tarb.begin();
tarb.erase(tarb.begin());
--cur;
}
}
}
vll minimum_closure_costs(int NX, vi U, vi V, vi W) {
n = NX;
vll ans(n);
for (int i = 0; i < n - 1; ++i) {
++U[i];
++V[i];
ans[0] += W[i];
g[U[i]].pub({ V[i],W[i] });
g[V[i]].pub({ U[i],W[i] });
}
dfs();
for (int k = 1; k < n; ++k) {
ans[k] = dp[1][k][0];
}
return ans;
}
/*
5
0 1 1
0 2 4
0 3 3
2 4 2
4
0 1 5
2 0 10
0 3 5
*/
# | 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... |