/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e5+100, M = 1e5+10, K = 20;
struct Edge{
int to, w;
};
struct Line{
bool type = false;
float x = 0;
ll m, c;
bool operator < (const Line &other) const{
if(type != other.type) return x < other.x;
return m < other.m;
}
};
int n, dist[N];
vector<Edge> g[N];
ll s[N], c[N], dp[N];
deque<Line> cht;
float intersection(const Line &a, const Line &b){
return (float) (a.c - b.c) / (b.m - a.m);
}
void calcX(const deque<Line>::iterator &a){
if(cht.size() > 1){
a->x = intersection(*prev(a), *a);
}
}
void pre(int v, int p){
for(auto U: g[v]){
int u = U.to;
ll w = U.w;
if(u != p){
dist[u] = dist[v] + w;
pre(u, v);
}
}
}
void dfs(int v, int p){
for(auto U: g[v]){
int u = U.to;
if(u != p){
vector<Line> popp;
Line j = *prev(upper_bound(all(cht), Line{1, (float) s[v], 0, 0}));
dp[u] = j.c + j.m * c[u] + s[u] + c[u] * dist[u];
Line L{0, 0, -dist[u], dp[u]};
while(cht.size() > 1 && intersection(cht[cht.size() - 2], cht.back()) >= intersection(cht.back(), L)){
popp.pb(cht.back());
cht.pop_back();
}
cht.pb(L);
calcX(prev(cht.end()));
dfs(u, v);
cht.pop_back();
reverse(all(popp));
for(auto &popped: popp) cht.pb(popped);
}
}
}
void solve(){
cin >> n;
for(int i = 0; i < n - 1; ++i){
int u, v, w; cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
for(int i = 2; i <= n; ++i) cin >> s[i] >> c[i];
dist[1] = 0;
pre(1, 0);
dp[1] = 0;
cht.pb({0, 0, 0, 0});
dfs(1, 0);
for(int i = 2; i <= n; ++i) cout << dp[i] << ' ';
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
harbingers.cpp: In function 'int main()':
harbingers.cpp:102:14: warning: unused variable 'aa' [-Wunused-variable]
102 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
3540 KB |
Output isn't correct |
3 |
Incorrect |
49 ms |
16888 KB |
Output isn't correct |
4 |
Incorrect |
72 ms |
23792 KB |
Output isn't correct |
5 |
Incorrect |
83 ms |
30792 KB |
Output isn't correct |
6 |
Runtime error |
111 ms |
37680 KB |
Memory limit exceeded |
7 |
Incorrect |
56 ms |
15300 KB |
Output isn't correct |
8 |
Incorrect |
187 ms |
21304 KB |
Output isn't correct |
9 |
Incorrect |
258 ms |
26568 KB |
Output isn't correct |
10 |
Incorrect |
224 ms |
24204 KB |
Output isn't correct |