# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
554518 | Zhora_004 | Road Closures (APIO21_roads) | C++17 | 0 ms | 0 KiB |
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 <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
#include <fstream>
//#include <bit>
//#include <ranges>
//#include <numbers>
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
/*ofstream fout("timeline.out");
ifstream fin("timeline.in");*/
using ll = long long;
using namespace std;
int n, k;
vector<int> a, b, c;
vector<vector<pair<int, int>>> tree;
vector<vector<ll>> dp;
bool task(int id)
{
if (id == 1)
{
for (int& i : a) if (i) return 0;
return 1;
}
if (id == 2)
{
for (int i = 0; i < n - 1; i++) if (a[i] != i || b[i] != i + 1) return 0;
return 1;
}
return 0;
}
void dfs(int u, int p, int w)
{
for (pair<int, int>& edge : tree[u]) if (edge.first != p) dfs(edge.first, u, edge.second);
int can = k;
dp[u][0] = 0;
if (p != -1) can--;
int cnt = sz(tree[u]);
if (p != -1) cnt--;
vector<vector<ll>> d(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf));
d[0][0] = 0;
int cur = 1;
for (int i = 0; i < sz(tree[u]); i++)
{
int v = tree[u][i].first, len = tree[u][i].second;
if (v == p) continue;
for (int j = 0; j <= min(cur, can); j++)
{
if (cur - 1 >= j) d[cur][j] = d[cur - 1][j] + dp[v][1];
if (j) d[cur][j] = min(d[cur][j], d[cur - 1][j - 1] + dp[v][0]);
}
cur++;
}
dp[u][0] = 1ll * inf * inf;
for (int j = 0; j <= can; j++) dp[u][0] = min(dp[u][0], d[cur - 1][j]);
if (p != -1)
{
can = k;
d = vector<vector<ll>>(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf));
d[0][0] = 0;
cur = 1;
for (int i = 0; i < sz(tree[u]); i++)
{
int v = tree[u][i].first, len = tree[u][i].second;
if (v == p) continue;
for (int j = 0; j <= min(cur, can); j++)
{
if (cur - 1 >= j) d[cur][j] = d[cur - 1][j] + dp[v][1];
if (j) d[cur][j] = min(d[cur][j], d[cur - 1][j - 1] + dp[v][0]);
}
cur++;
}
dp[u][1] = 1ll * inf * inf;
for (int j = 0; j <= can; j++) dp[u][1] = min(dp[u][1], d[cur - 1][j]);
dp[u][1] += w;
}
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = N;
a = U;
b = V;
c = W;
if (task(1))
{
vector<ll> res;
ll sum = 0;
for (int& i : c) sum += i;
res.push_back(sum);
sort(c.begin(), c.end());
reverse(c.begin(), c.end());
for (k = 0; k < n - 1; k++)
{
sum -= c[k];
res.push_back(sum);
}
return res;
}
if (task(2))
{
ll sum = 0;
dp = vector<vector<ll>>(n, vector<ll>(2));
for (int i = 1; i < n; i++)
{
dp[i][0] = dp[i - 1][1];
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + c[i - 1];
sum += c[i - 1];
}
vector<ll> res;
res.push_back(sum);
res.push_back(min(dp[n - 1][0], dp[n - 1][1]));
for (k = 2; k < n; k++) res.push_back(0);
return res;
}
else
{
ll tmp = 0;
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
u = a[i], v = b[i], w = c[i];
tree[u].push_back({ v, w });
tree[v].push_back({ u, w });
tmp += w;
}
vector<ll> res;
res.push_back(tmp);
for (k = 1; k < n; k++)
{
dp = vector<vector<ll>>(n, vector<ll>(2));
dfs(0, -1, -1);
res.push_back(dp[0][0]);
}
return res;
}
}