/* 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 = 1e6+100, M = 1e5+10, K = 20;
struct Edge{
int to;
ll w;
};
struct Line{
bool type = false;
double 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;
vector<Edge> g[N];
ll s[N], c[N], dist[N], dp[N];
set<Line> cht;
double intersection(const set<Line>::iterator &a, const set<Line>::iterator &b){
return (double) (a->c - b->c) / (b->m - a->m);
}
double intersection(const set<Line>::iterator &a, const Line &b){
return (double) (a->c - b.c) / (b.m - a->m);
}
void calcX(const set<Line>::iterator &a){
if(cht.size() > 1){
Line L = *a;
L.x = intersection(prev(a), a);
cht.insert(a, L);
}
}
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(cht.upper_bound({1, (double) 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(prev(prev(cht.end())), prev(cht.end())) <= intersection(prev(cht.end()), L)){
popp.pb(*prev(cht.end()));
cht.erase(prev(cht.end()));
}
auto it = cht.insert(L).first;
calcX(it);
auto val = *it;
dfs(u, v);
cht.erase(val);
for(auto &popped: popp) cht.insert(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.insert({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:108:14: warning: unused variable 'aa' [-Wunused-variable]
108 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
23816 KB |
Output isn't correct |
2 |
Incorrect |
15 ms |
24840 KB |
Output isn't correct |
3 |
Runtime error |
61 ms |
41560 KB |
Memory limit exceeded |
4 |
Runtime error |
94 ms |
49932 KB |
Memory limit exceeded |
5 |
Runtime error |
134 ms |
58812 KB |
Memory limit exceeded |
6 |
Runtime error |
175 ms |
65536 KB |
Memory limit exceeded |
7 |
Runtime error |
91 ms |
40468 KB |
Memory limit exceeded |
8 |
Runtime error |
136 ms |
48488 KB |
Memory limit exceeded |
9 |
Execution timed out |
1078 ms |
53016 KB |
Time limit exceeded |
10 |
Runtime error |
121 ms |
50664 KB |
Memory limit exceeded |