#include<bits/stdc++.h>
using namespace std;
#define ll long long int
const ll N = 2500+5 , INF = 1e18 , MOD = 1e9+7;
vector<pair<ll, ll> > adj[N];
vector<ll> dist(N);
void dfs1(ll s , ll p){
for(auto u : adj[s]){
if(u.first == p) continue;
dist[u.first] = dist[s]+u.second;
dfs1(u.first,s);
}
}
vector<ll> path,dp(N,INF),w(N),speed(N);
void dfs2(ll s , ll p){
for(auto u : path) dp[s] = min(dp[s], w[s] + (dist[s] - dist[u]) * speed[s] + dp[u]);
path.push_back(s);
for(auto u : adj[s]){
if(u.first == p) continue;
dfs2(u.first,s);
}
path.pop_back();
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll n; cin >> n;
for(ll i = 1; i < n; i++){
ll a, b, c; cin >> a >> b >> c;
adj[a].push_back(make_pair(b,c));
adj[b].push_back(make_pair(a,c));
}
for(ll i = 2; i <= n; i++) cin >> w[i] >> speed[i];
dist[1] = 0; dfs1(1, 0);
dp[1] = 0; dfs2(1, 0);
for(ll i = 2; i <= n; i++) cout << dp[i] << ' ';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
860 KB |
Output is correct |
3 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
4 |
Runtime error |
1 ms |
864 KB |
Execution killed with signal 11 |
5 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
6 |
Runtime error |
1 ms |
892 KB |
Execution killed with signal 11 |
7 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
8 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
9 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
10 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |