#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 1e5 + 69;
struct line {
long long k, n;
line(long long _k, long long _n) {
k = _k;
n = _n;
}
long long val(long long x) {
return x * k + n;
}
};
int n, s[N], w[N];
long long dp[N], dep[N];
vector<pair<int, int>> g[N];
vector<pair<line, int>> dq;
void dfs(int v, int p) {
int l = 1, r = dq.size() - 1, id = 0;
while (l <= r) {
int mid = (l + r) / 2;
if (dq[mid].f.val(w[v]) <= dq[mid - 1].f.val(w[v])) {
id = mid;
l = mid + 1;
}
else
r = mid - 1;
}
dp[v] = dq[id].f.val(w[v]) + s[v] + 1ll * w[v] * dep[v];
line ln = {-dep[v], dp[v]};
while (dq.size() > 1 && (ln.k - dq.back().f.k) * (dq.back().f.n - dq[dq.size() - 2].f.n) <= (dq.back().f.n - ln.n) * (dq[dq.size() - 2].f.k - dq.back().f.k)) {
dq.pop_back();
}
dq.push_back({ln, v});
for (auto u : g[v]) {
if (u.f == p)
continue;
dep[u.f] = dep[v] + u.s;
dfs(u.f, v);
}
dq.pop_back();
}
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];
dq.push_back({{0, 0}, 1});
for (auto j : g[1]) {
dep[j.f] = j.s;
dfs(j.f, 1);
}
for (int i = 2;i <= n;i++)
cout << dp[i] << ' ';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
9304 KB |
Execution killed with signal 11 |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
26 ms |
10676 KB |
Output is correct |
4 |
Correct |
35 ms |
13844 KB |
Output is correct |
5 |
Correct |
42 ms |
15708 KB |
Output is correct |
6 |
Correct |
68 ms |
18448 KB |
Output is correct |
7 |
Runtime error |
32 ms |
15956 KB |
Execution killed with signal 11 |
8 |
Runtime error |
62 ms |
26564 KB |
Execution killed with signal 11 |
9 |
Runtime error |
62 ms |
28684 KB |
Execution killed with signal 11 |
10 |
Runtime error |
51 ms |
29256 KB |
Execution killed with signal 11 |