/* 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];
vector<Line> cht;
float intersection(const Line &a, const Line &b){
return (float) (a.c - b.c) / (b.m - a.m);
}
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){
// cout << v << '\n';
// for(auto k: cht) cout << k.c << ' ' << k.m << ' ' << k.x << '\n';
// cout << '\n';
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) c[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]};
bool flag = 1;
while(cht.size() > 1){
if(cht.back().m == L.m){
if(cht.back().c >= L.m){
popp.pb(cht.back());
cht.pop_back();
}else{
flag = 0;
break;
}
}else if(intersection(cht[cht.size() - 2], cht.back()) >= intersection(cht.back(), L)){
popp.pb(cht.back());
cht.pop_back();
}else{
break;
}
}
if(flag){
cht.pb(L);
if(cht.size() > 1)
cht.back().x = intersection(cht[cht.size() - 2], cht.back());
}
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:117:14: warning: unused variable 'aa' [-Wunused-variable]
117 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
3284 KB |
Output isn't correct |
3 |
Incorrect |
36 ms |
12548 KB |
Output isn't correct |
4 |
Incorrect |
53 ms |
17168 KB |
Output isn't correct |
5 |
Incorrect |
79 ms |
22104 KB |
Output isn't correct |
6 |
Incorrect |
130 ms |
26612 KB |
Output isn't correct |
7 |
Incorrect |
54 ms |
12168 KB |
Output isn't correct |
8 |
Execution timed out |
1092 ms |
17020 KB |
Time limit exceeded |
9 |
Incorrect |
264 ms |
21280 KB |
Output isn't correct |
10 |
Incorrect |
384 ms |
19500 KB |
Output isn't correct |