# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
448725 | gcmango | Harbingers (CEOI09_harbingers) | C++17 | 171 ms | 65540 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>
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MXN = 100001;
int N;
ll dist[MXN], dp[MXN];
pll arr[MXN];
vector<pll> g[MXN];
double cross(int x, int y) { return 1.0 * (dp[y] - dp[x]) / (dist[y] - dist[x]); }
void dfs(int x, int p, ll d, vector<int> stk) {
dist[x] = d;
int s = 0, e = stk.size();
if (stk.size()) {
while (s + 1 < e && cross(stk[s], stk[s + 1]) <= arr[x].y) s++;
int y = stk[s];
dp[x] = arr[x].y * (dist[x] - dist[y]) + dp[y] + arr[x].x;
}
stk.push_back(x);
while (e > 1 && cross(stk[e - 2], stk[e - 1]) > cross(stk[e - 1], stk[e]))
stk[e - 1] = stk[e], e--, stk.pop_back();
//dp[x] = min(dp[x], dp[cur] + (dist[x] - dist[cur]) * arr[x].y + arr[x].x), cur = par[cur];
// dp[x] = min(dp[x], - dist[cur] * arr[x].y + dp[cur] + dist[x] * arr[x].y + arr[x].x);
for (auto &[n, m] : g[x])
if (n != p) dfs(n, x, d + m, stk);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int i = 0; i < N - 1; ++i) {
ll a, b, c; cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
for (int i = 2; i <= N; ++i) cin >> arr[i].x >> arr[i].y;
dfs(1, 0, 0, {});
for (int i = 2; i <= N; ++i) cout << dp[i] << ' ';
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |