이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
using ll = long long;
const int maxn = (int)2e3+10;
const ll LINF = (ll)1e18;
int n;
ll dp[maxn][maxn];
vector<pair<int,ll>> adj[maxn];
void dfs(int s, int p){
vector<pair<int,int>> v; v.clear();
for(auto x : adj[s]){
int u = x.fi, w = x.se;
if(u==p) continue;
dfs(u,s), v.pb({u,w});
dp[s][0]+=dp[u][0]+w;
}
if(sz(v)==0) return;
sort(all(v), [&](pair<int,int> a, pair<int,int> b){
return a.se<b.se;});
ll dp2[sz(v)+1][n+1];
for(int i = 0; i <= sz(v); i++)
for(int j = 0; j <= n; j++)
dp2[i][j] = LINF;
for(int i = 0; i < n; i++) dp2[0][i] = 0;
for(int i = 1; i <= sz(v); i++){
for(int j = 0; j < n; j++){
int x = v[i-1].fi, y = v[i-1].se;
if(j) dp2[i][j] = dp2[i][j-1];
dp2[i][j]=min(dp2[i][j],dp2[i-1][j]+dp[x][j]+y);
if(j) dp2[i][j]=min(dp2[i][j], dp2[i-1][j-1]+dp[x][j-1]);
}
}
for(int k = 1; k < n; k++)
dp[s][k] = min(dp[s][k-1], dp2[sz(v)][k]);
}
vector<ll> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
vector<ll> ans(N,0ll);
ans[0] = accumulate(all(W),0ll);
if(accumulate(all(U),0ll)==0ll){
sort(all(W)); reverse(all(W)); int i = 0;
ll tot = accumulate(all(W),0ll);
for(auto &u : ans) u=tot,tot-=W[i++];
return ans;
}
bool isLine = 1;
for(int i = 0; i < N-1; i++)
isLine &= (U[i]==i and V[i]==i+1);
if(isLine){
ll dp[N-1][2];
dp[0][0] = dp[0][1] = 0;
for(int i = 1; i < N; i++){
dp[i][0] = min(dp[i-1][1],dp[i-1][0])+W[i-1];
dp[i][1] = dp[i-1][0];
}
ans[1] = min(dp[N-1][0],dp[N-1][1]);
return ans;
}
for(int i = 0; i < N-1; i++){
adj[U[i]].pb({V[i],W[i]});
adj[V[i]].pb({U[i],W[i]});
}
dfs(0,-1); int i = 0;
for(auto &u : ans) u = dp[0][i++];
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... |