# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
965338 | MisterReaper | Harbingers (CEOI09_harbingers) | C++17 | 3 ms | 5208 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using i64 = long long;
constexpr int maxN = 1E5 + 5;
constexpr i64 INF = 1E18;
int N;
std::vector<std::pair<int, int>> adj[maxN];
int S[maxN], V[maxN], par[maxN], D[maxN];
i64 dp[maxN];
void dfs(int v) {
int u = v;
while(u != 1) {
u = par[u];
// std::cerr << u << ' ' << v << ' ' << dp[u] + 1LL * D[u] * V[v] + S[v] << '\n';
dp[v] = std::min(dp[v], dp[u] + 1LL * (D[v] - D[u]) * V[v] + S[v]);
}
for(auto [u, w] : adj[v]) {
if(u == par[v]) {
continue;
}
D[u] = D[v] + w;
par[u] = v;
dfs(u);
}
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#ifndef LOCAL
freopen("harbingers.in", "r", stdin);
freopen("harbingers.out", "w", stdout);
#endif
std::cin >> N;
for(int i = 1; i <= N - 1; i++) {
int A, B, W;
std::cin >> A >> B >> W;
adj[A].emplace_back(B, W);
adj[B].emplace_back(A, W);
}
for(int i = 2; i <= N; i++) {
std::cin >> S[i] >> V[i];
}
std::fill_n(&dp[1], N, INF);
dp[1] = 0;
par[1] = 1;
dfs(1);
for(int i = 2; i <= N; i++) {
std::cout << dp[i] << ' ';
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |