#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
struct Line {
mutable ll k, m, p;
bool operator<(const Line &l) const { return k < l.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<> > {
static const ll INF = LLONG_MAX;
ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
bool intersect(iterator x, iterator y) {
if(y == end()) {
x->p = INF;
return 0;
}
if(x->k == y->k) x->p = (x->m > y->m) ? INF : -INF;
else x->p = div(y->m - x->m, x->k - y->k);
return (x->p >= y->p);
}
void add(ll k, ll m) {
auto z = insert({ k, m, 0 });
auto y = z++;
auto x = y;
while(intersect(y, z)) z = erase(z);
if(x != begin() && intersect(--x, y))
intersect(x, y = erase(y));
while((y = x) != begin() && (--x)->p >= y->p)
intersect(x, erase(y));
}
ll query(ll x) {
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
int n;
vector<vector<pii> > graph;
pii H[maxn];
vector<int> st;
ll dp[maxn], dist[maxn];
LineContainer lc;
void dfs1(int u, int p) {
dp[u] = H[u].first + H[u].second * dist[u];
//H[u].first + H[u].second * dist[u] - (H[u].second * dist[id] - dp[id])
for(int &id : st)
dp[u] = min(dp[u], H[u].first + H[u].second * (dist[u] - dist[id]) + dp[id]);
st.push_back(u);
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
dist[v] = dist[u] + w;
dfs1(v, u);
}
st.pop_back();
}
void dfs2(int u, int p) {
if(u != 1) {
dp[u] = H[u].first + H[u].second * dist[u] - lc.query(H[u].second);
lc.add(dist[u], -dp[u]);
}
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
dist[v] = dist[u] + w;
dfs2(v, u);
}
}
int32_t main() {
cin >> n;
graph.resize(n+1);
for(int i=1; i<=n; i++) dp[i] = 1e18;
dp[1] = 0;
for(int i=0; i<n-1; i++) {
int a, b, w;
cin >> a >> b >> w;
graph[a].push_back({ b, w });
graph[b].push_back({ a, w });
}
for(int i=2; i<=n; i++) cin >> H[i].first >> H[i].second;
if(n <= 2500) {
dfs1(1, 0);
} else {
lc.add(0, 0);
dfs2(1, 0);
}
for(int i=2; i<=n; i++) cout << dp[i] << " ";
return 0;
}
Compilation message
harbingers.cpp: In function 'void dfs1(int, int)':
harbingers.cpp:81:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | for(auto &[v, w] : graph[u]) {
| ^
harbingers.cpp: In function 'void dfs2(int, int)':
harbingers.cpp:96:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | for(auto &[v, w] : graph[u]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
8 ms |
2652 KB |
Output is correct |
3 |
Correct |
56 ms |
8256 KB |
Output is correct |
4 |
Correct |
78 ms |
11348 KB |
Output is correct |
5 |
Correct |
100 ms |
12628 KB |
Output is correct |
6 |
Correct |
155 ms |
15120 KB |
Output is correct |
7 |
Incorrect |
85 ms |
9044 KB |
Output isn't correct |
8 |
Incorrect |
136 ms |
14652 KB |
Output isn't correct |
9 |
Incorrect |
123 ms |
13748 KB |
Output isn't correct |
10 |
Incorrect |
134 ms |
15852 KB |
Output isn't correct |