# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
488785 | Theo830 | 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///APIO 2021 vc
#include "roads.h"
vector<vector<ii> >adj;
ll dp[2005][2];
ll solve(ll idx,ll k,ll p,bool ep){
if(dp[idx][ep] != -1){
return dp[idx][ep];
}
ll ans = INF;
ll siz = k - ep;
vll ex;
ll sum = 0;
for(auto x:adj[idx]){
if(x.F != p){
sum += x.S + solve(x.F,k,idx,0);
ex.pb(solve(x.F,k,idx,1) - (x.S + solve(x.F,k,idx,0)));
}
}
ans = sum;
sort(all(ex));
f(i,0,min((1LL) * ex.size(),siz)){
ans += ex[i];
}
return dp[idx][ep] = ans;
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,std::vector<int> V,std::vector<int> W){
vll ans;
adj.assign(N+5,vector<ii>());
f(i,0,N-1){
adj[U[i]].pb(ii(V[i],W[i]));
adj[V[i]].pb(ii(U[i],W[i]));
}
f(k,0,N){
memset(dp,-1,sizeof dp);
ans.pb(solve(1,k,0,0));
}
return ans;
}