#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
int n;
vector<vector<pii> > graph;
pii H[maxn];
vector<int> st;
ll dp[maxn], dist[maxn];
void dfs(int u, int p) {
dp[u] = H[u].first + H[u].second * dist[u];
for(int &id : st)
dp[u] = min(dp[u], H[u].first + H[u].second * (dist[u] - dist[id]) + dp[id]);
st.push_back(u);
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
dist[v] = dist[u] + w;
dfs(v, u);
}
st.pop_back();
}
int32_t main() {
cin >> n;
graph.resize(n+1);
for(int i=1; i<=n; i++) dp[i] = 1e18;
dp[1] = 0;
for(int i=0; i<n-1; i++) {
int a, b, w;
cin >> a >> b >> w;
graph[a].push_back({ b, w });
graph[b].push_back({ a, w });
}
for(int i=2; i<=n; i++) cin >> H[i].first >> H[i].second;
dfs(1, 0);
for(int i=2; i<=n; i++) cout << dp[i] << " ";
return 0;
}
Compilation message
harbingers.cpp: In function 'void dfs(int, int)':
harbingers.cpp:35:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
35 | for(auto &[v, w] : graph[u]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
2668 KB |
Output is correct |
3 |
Execution timed out |
1080 ms |
8236 KB |
Time limit exceeded |
4 |
Execution timed out |
1055 ms |
9796 KB |
Time limit exceeded |
5 |
Execution timed out |
1073 ms |
11276 KB |
Time limit exceeded |
6 |
Execution timed out |
1049 ms |
12816 KB |
Time limit exceeded |
7 |
Execution timed out |
1068 ms |
10188 KB |
Time limit exceeded |
8 |
Execution timed out |
1046 ms |
12892 KB |
Time limit exceeded |
9 |
Execution timed out |
1041 ms |
12980 KB |
Time limit exceeded |
10 |
Execution timed out |
1027 ms |
12556 KB |
Time limit exceeded |