Submission #924712

#TimeUsernameProblemLanguageResultExecution timeMemory
924712OAleksaHarbingers (CEOI09_harbingers)C++14
20 / 100
54 ms32980 KiB
#include <bits/stdc++.h> using namespace std; #define f first #define s second #define int long long const long long inf = 1e9; const int N = 1e5 + 69; const int M = N; struct Line { long long k; long long n; Line(long long _k = 0, long long _n = inf) { k = _k; n = _n; } long long val(long long x) { return k * x + n; } } st[M]; int lc[M], rc[M], root[N], node; vector<pair<int, int>> g[N]; int s[N], w[N], n; long long dp[N], dep[N]; void AddLine(int v, int p, int l, int r, Line ln) { long long mid = (l + r) / 2; st[v] = st[p]; if (st[v].val(mid) > ln.val(mid)) swap(st[v], ln); if (l == r) return; if (ln.val(l) < st[v].val(l)) { lc[v] = ++node; rc[v] = rc[p]; AddLine(lc[v], lc[p], l, mid, ln); } else { rc[v] = ++node; lc[v] = lc[p]; AddLine(rc[v], rc[p], mid + 1, r, ln); } } long long Query(int v, int l, int r, int x) { if (l == r) return st[v].val(x); else { int mid = (l + r) / 2; if (x <= mid) return min(st[v].val(x), Query(lc[v], l, mid, x)); else return min(st[v].val(x), Query(rc[v], mid + 1, r, x)); } } void dfs(int v, int p) { dp[v] = min(dep[v] * w[v] + s[v], Query(root[p], 1, inf, w[v]) + dep[v] * w[v] + s[v]); root[v] = ++node; Line ln = {-dep[v], dp[v]}; AddLine(root[v], root[p], 1, inf, ln); for (auto u : g[v]) { if (u.f == p) continue; dep[u.f] = dep[v] + u.s; dfs(u.f, v); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n; for (int i = 1;i <= n - 1;i++) { int a, b, c; cin >> a >> b >> c; g[a].push_back({b, c}); g[b].push_back({a, c}); } for (int i = 2;i <= n;i++) cin >> s[i] >> w[i]; dfs(1, 0); for (int i = 2;i <= n;i++) cout << dp[i] << " "; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...