제출 #667319

#제출 시각아이디문제언어결과실행 시간메모리
667319radalFireworks (APIO16_fireworks)C++17
7 / 100
1 ms980 KiB
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,avx2") #pragma GCC optimize("unroll-loops,O2") #define rep(i,l,r) for (int i = l; i < r; i++) #define repr(i,r,l) for (int i = r; i >= l; i--) #define X first #define Y second #define all(x) (x).begin() , (x).end() #define pb push_back #define endl '\n' #define debug(x) cerr << #x << " : " << x << endl; using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pll; constexpr int N = 5e3+15,mod = 1e9+7; constexpr ll inf = 1e18+10; inline int mkay(int a,int b){ if (a+b >= mod) return a+b-mod; // if (a+b < 0) return a+b+mod; return a+b; } inline int poww(int a,int k){ if (k < 0) return 0; int z = 1; while (k){ if (k&1) z = 1ll*z*a%mod; a = 1ll*a*a%mod; k >>= 1; } return z; } vector<pll> adj[N]; vector<ll> ve[N]; ll h[N]; int n,m; ll dp[N][N]; void pre(int v){ if (v > n) ve[v].pb(h[v]); for (pll p : adj[v]){ int u = p.X; h[u] = h[v]+p.Y; pre(u); for (ll x : ve[u]) ve[v].pb(x); } sort(all(ve[v])); ve[v].resize(unique(all(ve[v]))-ve[v].begin()); } void dfs(int v){ for (auto [u,w] : adj[v]) dfs(u); int sz = ve[v].size(); rep(i,0,sz){ ll x = ve[v][i]-h[v]; if (v > n && x){ dp[v][i] = inf; continue; } dp[v][i] = 0; for (auto [u,w] : adj[v]){ ll mi = inf; int sz2 = ve[u].size(); if (!sz2) mi = 0; rep(j,0,sz2){ ll y = ve[u][j]-h[u]; if (y > x) break; ll cost; if (y+w <= x) cost = x-y-w; else cost = y+w-x; mi = min(mi,dp[u][j]+cost); } dp[v][i] += mi; } } } int main(){ ios :: sync_with_stdio(0); cin.tie(0); cin >> n >> m; rep(i,2,n+m+1){ int p,w; cin >> p >> w; adj[p].pb({i,w}); } pre(1); ll out = inf; dfs(1); int sz = ve[1].size(); rep(i,0,sz) out = min(out,dp[1][i]); cout << out; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...