#include <bits/stdc++.h>
using namespace std;
const int nax = 1e5 + 5;
struct line {
long long m, b;
long long eval (long long x) {
return x * m + b;
}
pair<long long, long long> intersect (line ln) {
// m1 * x + b1 = m2 * x = b2
return {ln.b - b, m - ln.m};
}
};
bool check (line x, line y, line z) {
pair<long long, long long> p1 = x.intersect (y), p2 = y.intersect (z);
return (__int128) p1.first * p2.second >= (__int128) p2.first * p1.second;
}
struct convex_hull {
vector<line> st;
void insert (line ln) {
if (st.empty ()) {
st.push_back (ln);
return;
}
while (st.size () >= 2 && check (st[st.size () - 2], st.back (), ln)) {
st.pop_back ();
}
st.push_back (ln);
}
long long query (long long x) {
int l = 0, r = (int) st.size () - 2;
long long ans = 2e18;
while (l <= r) {
int mid = (l + r) / 2;
pair<long long, long long> p1 = st[mid].intersect (st[mid + 1]);
if (p1.first < x * p1.second) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return st[r + 1].eval (x);
}
};
long long start[nax], per_km[nax];
long long ans[nax];
int sz[nax], heavy[nax], parent[nax], head[nax], cost_heavy[nax];
convex_hull hulls[nax];
vector<pair<int, int>> edges[nax];
void dfs (int u, int p) {
sz[u] = 1;
int mx = 0;
parent[u] = p;
for (auto &it : edges[u]) {
int v = it.first;
if (v != p) {
dfs (v, u);
if (sz[v] > mx) {
mx = sz[v];
heavy[u] = v;
cost_heavy[u] = it.second;
}
}
}
}
void decompose (int u, int p, int h, long long path) {
head[u] = h;
ans[u] = start[u] + path * per_km[u] + hulls[h].query (per_km[u]);
int node = parent[head[u]];
while (node) {
ans[u] = min (ans[u], start[u] + path * per_km[u] + hulls[head[node]].query (per_km[u]));
node = parent[head[node]];
}
hulls[h].insert (line {-path, ans[u]});
for (auto &it : edges[u]) {
if (it.first != p && it.first != heavy[u]) {
int v = it.first;
hulls[v].st.push_back ({0, 0});
decompose (v, u, v, path + it.second);
}
}
if (heavy[u]) {
decompose (heavy[u], u, h, path + cost_heavy[u]);
}
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v, x;
cin >> u >> v >> x;
edges[u].push_back ({v, x});
edges[v].push_back ({u, x});
}
for (int i = 2; i <= n; ++i) {
cin >> start[i] >> per_km[i];
}
dfs (1, 0);
hulls[1].st.push_back ({0, 0});
decompose (1, 0, 1, 0);
for (int i = 2; i <= n; ++i) {
cout << ans[i] << ' ';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
Compilation message
harbingers.cpp: In member function 'long long int convex_hull::query(long long int)':
harbingers.cpp:43:19: warning: unused variable 'ans' [-Wunused-variable]
43 | long long ans = 2e18;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 KB |
Output is correct |
3 |
Correct |
31 ms |
15504 KB |
Output is correct |
4 |
Correct |
38 ms |
18368 KB |
Output is correct |
5 |
Correct |
68 ms |
21076 KB |
Output is correct |
6 |
Correct |
66 ms |
23672 KB |
Output is correct |
7 |
Correct |
43 ms |
16156 KB |
Output is correct |
8 |
Correct |
82 ms |
21700 KB |
Output is correct |
9 |
Execution timed out |
1036 ms |
21932 KB |
Time limit exceeded |
10 |
Correct |
72 ms |
22852 KB |
Output is correct |