#include <bits/stdc++.h>
using namespace std;
const int N = 100'000 + 1;
int n;
vector<pair<int, int>> ad[N];
int s[N], velocity[N];
int d[N];
struct Line {
long long a, b;
double x(const auto& rhs) { return 1.0 * (rhs.b - b) / (a - rhs.a); }
};
struct CHT {
// vector<Line> hull;
int top;
array<Line, N> hull;
vector<pair<int, Line>> save;
CHT() : top(0) { hull[top = 1] = {0, 0}; }
void add(Line line) {
while (top > 1 && hull[top].x(hull[top - 1]) >= hull[top].x(line)) {
top -= 1;
save.push_back({1, hull.end()[-1]});
}
save.push_back({-1, line});
hull[++top] = line;
}
long long query(int u) {
auto cal = [&](int it) {
return hull[it].a * velocity[u] + hull[it].b + 1ll * d[u] * velocity[u] + s[u];
};
int l = 0, r = hull.size() - 2, it = 0;
while (l <= r) {
int mid = l + r >> 1;
if (cal(mid) >= cal(mid + 1)) l = it = mid + 1;
else r = mid - 1;
}
return cal(it);
}
} convex;
long long dp[N];
void dfs(int u, int p = 0) {
auto& ret = dp[u];
ret = min(ret, convex.query(u));
convex.add({-d[u], ret});
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
d[v] = d[u] + w;
dfs(v, u);
}
vector<Line> toBeAdded;
bool pass = false;
while (convex.save.size()) {
auto [t, line] = convex.save.end()[-1];
if (t == -1) {
if (pass) break;
convex.top -= 1;
pass = true;
} else toBeAdded.push_back(line);
convex.save.pop_back();
}
for (const auto& line : toBeAdded) convex.hull[++convex.top] = line;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
int u, v, w; cin >> u >> v >> w;
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
for (int i = 2; i <= n; ++i) cin >> s[i] >> velocity[i];
memset(dp, 40, sizeof dp);
dfs(1);
for (int i = 2; i <= n; ++i) cout << dp[i] << " \n"[i == n];
}
Compilation message
harbingers.cpp:13:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
13 | double x(const auto& rhs) { return 1.0 * (rhs.b - b) / (a - rhs.a); }
| ^~~~
harbingers.cpp: In member function 'long long int CHT::query(int)':
harbingers.cpp:37:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6236 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
6940 KB |
Output isn't correct |
3 |
Incorrect |
40 ms |
16460 KB |
Output isn't correct |
4 |
Incorrect |
42 ms |
20280 KB |
Output isn't correct |
5 |
Incorrect |
70 ms |
25848 KB |
Output isn't correct |
6 |
Incorrect |
83 ms |
29252 KB |
Output isn't correct |
7 |
Incorrect |
46 ms |
14368 KB |
Output isn't correct |
8 |
Incorrect |
75 ms |
18524 KB |
Output isn't correct |
9 |
Incorrect |
81 ms |
22040 KB |
Output isn't correct |
10 |
Incorrect |
66 ms |
20392 KB |
Output isn't correct |