#include<bits/stdc++.h>
#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"
using namespace std;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int N = 2e5 + 15;
const ll INF = 1e18;
int n;
ll dp[N];
int par[N];
vector<pii> g[N];
int wait[N], speed[N];
int h[N];
void dfs(int u, int p){
if(u > 1){
int v = par[u];
while(v){
minimize(dp[u], 1LL*speed[u]*(h[u] - h[v]) + wait[u] + dp[v]);
v = par[v];
}
}
for(auto [v, w] : g[u]) if(v != p){
h[v] = h[u] + w;
par[v] = u;
dfs(v,u);
}
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int u, v, w; cin >> u >> v >> w;
g[u].pub({v,w});
g[v].pub({u,w});
}
for(int i = 2; i <= n; i++) cin >> wait[i] >> speed[i];
fill(dp + 1, dp + 1 + n, INF);
dp[1] = 0;
dfs(1,-1);
for(int i = 2; i <= n; i++) cout << dp[i] << " ";
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".ANS", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
harbingers.cpp: In function 'int main()':
harbingers.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(task".ANS", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |