# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
488785 | Theo830 | 도로 폐쇄 (APIO21_roads) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}