Submission #448727

# Submission time Handle Problem Language Result Execution time Memory
448727 2021-08-01T05:08:27 Z gcmango Harbingers (CEOI09_harbingers) C++17
40 / 100
1000 ms 25468 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];
vector<int> stk;
stack<vector<int>> st;

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) {
    dist[x] = d;

    int s = 0, e = stk.size();
    if (e) {
        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);

    if (g[x].size() > 2) st.push(stk);

    for (auto &[n, m] : g[x]) {
        if (n == p) continue;
        if (g[x].size() > 2) stk = st.top();
        dfs(n, x, d + m);
    }

    if (g[x].size() > 2) st.pop();
}

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 Incorrect 2 ms 2636 KB Output isn't correct
2 Correct 4 ms 3240 KB Output is correct
3 Correct 610 ms 13028 KB Output is correct
4 Execution timed out 1094 ms 13228 KB Time limit exceeded
5 Correct 97 ms 20164 KB Output is correct
6 Correct 116 ms 24308 KB Output is correct
7 Incorrect 70 ms 12176 KB Output isn't correct
8 Execution timed out 1094 ms 15664 KB Time limit exceeded
9 Execution timed out 1076 ms 25468 KB Time limit exceeded
10 Execution timed out 1087 ms 16940 KB Time limit exceeded