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 <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 2000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
int n;
std::vector <std::pair <int,int>> g[MAXN];
bool bl[MAXN][MAXN][2];
llong dp[MAXN][MAXN][2];
void firstDFS(int node, int par)
{
for (auto &curr : g[node])
{
if (curr.first == par)
{
std::swap(curr, g[node].back());
g[node].pop_back();
break;
}
}
for (const auto &[u, w] : g[node])
{
firstDFS(u, node);
}
}
llong f(int node, int k, bool k2)
{
// if (k == 1) std::cout << "call: " << node << ' ' << g[node].size() << ' ' << k << '\n';
if (g[node].empty())
{
return 0;
}
if (bl[node][k][k2])
{
return dp[node][k][k2];
}
bl[node][k][k2] = true;
std::vector <llong> vals;
for (const auto &[u, w] : g[node])
{
dp[node][k][k2] += w + f(u, k + k2, false);
vals.push_back(f(u, k + k2 - 1, true) - (w + f(u, k + k2, false)));
}
std::sort(vals.begin(), vals.end());
for (int i = 0 ; i < std::min((int)vals.size(), k) ; ++i)
{
if (vals[i] >= 0) break;
dp[node][k][k2] += vals[i];
}
return dp[node][k][k2];
}
std::vector <llong> minimum_closure_costs(int N, std::vector <int> U, std::vector <int> V, std::vector <int> W)
{
n = N;
for (int i = 0 ; i < n - 1 ; ++i)
{
g[U[i] + 1].push_back({V[i] + 1, W[i]});
g[V[i] + 1].push_back({U[i] + 1, W[i]});
}
firstDFS(1, 0);
std::vector <llong> ans(n);
for (int i = 0 ; i < n ; ++i)
{
ans[i] = f(1, i, false);
}
return ans;
}
# | 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... |