#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>
const ll mod = 1e9 + 7;
const ll MAXN = 1e5 + 5;
const int oo = 1e18;
const int base = 320;
int n, s[MAXN], p[MAXN], d[MAXN];
vector<pii> adj[MAXN];
ll f[MAXN];
vector<int> A, B, xoaA[MAXN], xoaB[MAXN];
bool bad(int l1, int l2, int l3){
return (B[l3]-B[l1])*(A[l1]-A[l2])<(B[l2]-B[l1])*(A[l1]-A[l3]);
}
void add(int a, int b, int u){
// if(u==7){
// for(auto x:A){
// cout << x << ' ';
// }
// cout << '\n';
// }
A.push_back(a);
B.push_back(b);
while((int)A.size()>=3 && bad(A.size()-3, A.size()-2, A.size()-1)){
xoaA[u].push_back(A[A.size()-2]);
xoaB[u].push_back(B[B.size()-2]);
A.erase(A.end()-2);
B.erase(B.end()-2);
}
// if(u==9){
// for(auto x:A){
// cout << x << ' ';
// }
// cout << '\n';
// for(auto x:B){
// cout << x << ' ';
// }
// cout << '\n';
// }
}
void hoi(int a, int b, int u){
A.pop_back();
B.pop_back();
FOD(i, (int)xoaA[u].size()-1, 0){
A.push_back(xoaA[u][i]);
B.push_back(xoaB[u][i]);
}
xoaA[u].clear();
xoaB[u].clear();
// if(u==7){
// for(auto x:A){
// cout << x << ' ';
// }
// cout << '\n';
// }
}
ll query(int x){
int l=1, r=(int)A.size()-1, id=1;
if(r==-1){
return 0;
}
if(r==0){
return A[0]*x+B[0];
}
ll ans=oo;
while(l<=r){
int mid=(l+r)/2;
if(A[mid]*x+B[mid]<A[mid-1]*x+B[mid-1]){
id=mid;
l=mid+1;
ans=min(ans, (ll)(A[mid]*x+B[mid]));
}
else{
r=mid-1;
ans=min(ans, (ll)(A[mid-1]+B[mid-1]));
}
}
// if(x==6)cout << id << '\n';
ans=min({ans, (ll)(A[id]*x+B[id]), (ll)(A[id-1]*x+B[id-1])});
if(id!=A.size()-1){
ans=min(ans, (ll)(A[id+1]*x+B[id+1]));
}
return ans;
}
void dfs2(int u, int last){
if(u!=1){
f[u]=query(p[u])+d[u]*p[u]+s[u];
// if(u==4) cout << query(p[u]) << ' ';
}
add(-d[u], f[u], u);
for(auto [v, c]:adj[u]){
if(v==last) continue;
d[v]=d[u]+c;
dfs2(v, u);
}
hoi(-d[u], f[u], u);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(".INP", "r"))
{
freopen(".INP", "r", stdin);
freopen(".OUT", "w", stdout);
}
cin >> n;
FOR(i, 1, n-1){
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
FOR(i, 2, n){
cin >> s[i] >> p[i];
}
dfs2(1, 1);
FOR(i, 2, n){
cout << f[i] << ' ';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
harbingers.cpp:14:16: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
14 | const int oo = 1e18;
| ^~~~
harbingers.cpp: In function 'int main()':
harbingers.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen(".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
harbingers.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |