Submission #448725

# Submission time Handle Problem Language Result Execution time Memory
448725 2021-08-01T04:57:37 Z gcmango Harbingers (CEOI09_harbingers) C++17
40 / 100
171 ms 65540 KB
#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
1 Correct 2 ms 2636 KB Output is correct
2 Correct 5 ms 3320 KB Output is correct
3 Runtime error 107 ms 65540 KB Execution killed with signal 9
4 Runtime error 135 ms 65540 KB Execution killed with signal 9
5 Correct 112 ms 30228 KB Output is correct
6 Runtime error 142 ms 35684 KB Memory limit exceeded
7 Correct 74 ms 16324 KB Output is correct
8 Runtime error 171 ms 65540 KB Execution killed with signal 9
9 Runtime error 151 ms 65540 KB Execution killed with signal 9
10 Runtime error 147 ms 65540 KB Execution killed with signal 9