#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
const int maxn=1e5+5;
struct line{
int a,b;
line(int x=0,int y=inf)
{
a=x;
b=y;
}
int operator()(int x) const
{
return a*x+b;
}
};
struct node{
line d;
node *L,*R;
node()
{
L=NULL;
R=NULL;
d=line();
}
node(line x)
{
L=NULL;
R=NULL;
d=x;
}
node(node *&cur)
{
d=cur->d;
L=cur->L;
R=cur->R;
}
};
struct lichao{
node *root;
int ml=0,mr=1e4;
lichao()
{
root=new node();
}
void update(node *&cur,line newl,int l,int r)
{
if(l>r) return;
if(cur==NULL) cur=new node();
else cur=new node(cur);
int mid=l+r>>1;
if(newl(mid)<cur->d(mid)) swap(newl,cur->d);
if(newl(mid)>1e16) return;
if(newl.a<cur->d.a)
{
// if(cur->R)cur->R=new node(cur->R);
// else cur->R=new node();
update(cur->R,newl,mid+1,r);
}
else
{
// if(cur->L)cur->L=new node(cur->L);
// else cur->L=new node();
update(cur->L,newl,l,mid-1);
}
}
void update(line newl)
{
update(root,newl,ml,mr);
}
int get(node *&cur,int x,int l,int r)
{
if(l>x||r<x||cur==NULL) return inf;
int mid=l+r>>1;
if(x>mid) return min(cur->d(x),get(cur->R,x,mid+1,r));
else return min(cur->d(x),get(cur->L,x,l,mid-1));
}
int get(int x)
{
return get(root,x,ml,mr);
}
} town[maxn];
int h[maxn],n,dp[maxn],S[maxn],V[maxn];
line Line[maxn];
vector<pair<int,int>>g[maxn];
void dfs(int u,int p)
{
town[u].root=new node(town[p].root);
dp[u]=town[p].get(V[u])+h[u]*V[u]+S[u];
// cout<<u<<' '<<town[p].get(V[u])<<' '<<h[u]<<' '<<V[u]<<'\n';
town[u].update({-h[u],dp[u]});
for(pair<int,int>v:g[u])
{
if(v.first==p) continue;
h[v.first]=h[u]+v.second;
dfs(v.first,u);
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for(int i=1;i<n;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
for(int i=2;i<=n;i++) cin>>S[i]>>V[i];
town[1].update({0,0});
dfs(1,1);
for(int i=2;i<=n;i++) cout<<dp[i]<<' ';
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |