#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod =1e9 + 7, pr = 31;
const int mxn = 1e5 + 5, mxq = 1e5 + 5, sq = 1005, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e18 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
struct Line{
ll m, c;
Line (){
m = c = 0;
}
Line(ll _m, ll _c){
m = _m; c = _c;
}
ll eval(ll x){
return(x * m + c);
}
ld intersect(const Line &b){
return((ld)(b.c - c) / (m - b.m));
}
};
struct E{
int siz, pos;
Line v;
};
int n;
ll dp[mxn + 1], v[mxn + 1], c[mxn + 1];
Line dq[mxn + 1];
int siz = 0;
vt<pair<int, ll>>adj[mxn + 1];
vt<E>past;
ll get(ll x){
int l = 1, r = siz - 1, ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(dq[mid - 1].intersect(dq[mid]) <= x){
ans = mid; l = mid + 1;
}else{
r = mid - 1;
}
}
return(dq[ans].eval(x));
}
void upd(Line v){
int l = 1, r = siz - 1, ans = siz;
while(l <= r){
int mid = (l + r) >> 1;
if(dq[mid - 1].intersect(dq[mid]) >= dq[mid - 1].intersect(v)){
ans = mid; r = mid - 1;
}else{
l = mid + 1;
}
}
past.pb({siz, ans, dq[ans]});
dq[ans] = v; siz = ans + 1;
}
void rollback(){
auto [psiz, pans, pv] = past.back(); past.pop_back();
siz = psiz; dq[pans] = pv;
}
void dfs(int s, int pre, ll dep){
//dp[s] = min(dp[i] + (dep[s] - dep[i]) * v[s]);
if(s != 1)dp[s] = get(v[s]) + dep * v[s] + c[s];
//cout << s << " " << -dep << " " << dp[s] << "\n";
upd(Line(-dep, dp[s]));
for(auto [v, w]: adj[s]){
if(v != pre){
dfs(v, s, dep + w);
rollback();
}
}
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int u, v, d; cin >> u >> v >> d;
adj[u].pb(mpp(v, d)); adj[v].pb(mpp(u, d));
}
for(int i = 2; i <= n; i++)cin >> c[i] >> v[i];
dfs(1, -1, 0);
for(int i = 2; i <= n; i++)cout << dp[i] << " ";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("FOOD.inp", "r", stdin);
//freopen("FOOD.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
Compilation message
harbingers.cpp: In function 'void rollback()':
harbingers.cpp:81:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | auto [psiz, pans, pv] = past.back(); past.pop_back();
| ^
harbingers.cpp: In function 'void dfs(int, int, long long int)':
harbingers.cpp:89:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
89 | for(auto [v, w]: adj[s]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
3 ms |
5272 KB |
Output is correct |
3 |
Correct |
29 ms |
13004 KB |
Output is correct |
4 |
Correct |
44 ms |
16584 KB |
Output is correct |
5 |
Correct |
54 ms |
20780 KB |
Output is correct |
6 |
Correct |
74 ms |
23748 KB |
Output is correct |
7 |
Correct |
39 ms |
14288 KB |
Output is correct |
8 |
Correct |
79 ms |
19484 KB |
Output is correct |
9 |
Correct |
78 ms |
20540 KB |
Output is correct |
10 |
Correct |
68 ms |
19512 KB |
Output is correct |