Submission #970903

#TimeUsernameProblemLanguageResultExecution timeMemory
970903vjudge1Fireworks (APIO16_fireworks)C++14
26 / 100
3 ms2140 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int N=300+10, inf=1e18; int n, m, f[N][N], d[N]; vector<pair<int, int>> g[N]; void pre_dfs(int u){ for (auto &e:g[u]){ d[e.first]=d[u]+e.second; pre_dfs(e.first); } } void dfs(int u){ if (g[u].empty()){ f[u][d[u]]=0; return; } for (int i=0; i<N; ++i) f[u][i]=0; for (auto &e:g[u]){ int v=e.first, w=e.second; dfs(v); deque<int> dq; for (int i=0; i<N+w; ++i){ while (dq.size() && i-dq.front()>w) dq.pop_front(); if (i<N){ while (dq.size() && f[v][dq.back()]+dq.back()>=f[v][i]+i) dq.pop_back(); dq.push_back(i); } if (i>=w) f[v][i-w]=min(f[v][i-w], f[v][dq.front()]+dq.front()-(i-w)); } for (int i=1; i<N; ++i) f[v][i]=min(f[v][i], f[v][i-1]+1); for (int i=0; i<N; ++i) f[u][i]=min(inf, f[u][i]+f[v][i]); } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; if (n==1){ vector<int> v; for (int i=2; i<=n+m; ++i){ int p, c; cin >> p >> c; v.push_back(c); } sort(v.begin(), v.end()); int val=v[m/2], ans=0; for (auto &i:v) ans+=abs(i-val); cout << ans << '\n'; return 0; } for (int i=2; i<=m+n; ++i){ int p, c; cin >> p >> c; g[p].emplace_back(i, c); } for (int i=0; i<N; ++i) for (int j=0; j<N; ++j) f[i][j]=inf; pre_dfs(1); dfs(1); cout << *min_element(f[1], f[1]+N) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...