Submission #936235

# Submission time Handle Problem Language Result Execution time Memory
936235 2024-03-01T12:57:59 Z VMaksimoski008 Harbingers (CEOI09_harbingers) C++14
20 / 100
150 ms 26168 KB
#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;

//cht for min
struct CHT {
    struct Line {
        ll a, b;
        Line(ll x, ll y): a(x), b(y) {}

        ll val(ll x) {
            return a * x + b;
        }

        ll intersect(Line y) {
            return (y.b - b + a - y.a) / (a - y.a);
        }
    };

    deque<pair<Line, ll> > q;

    void insert(ll a, ll b) {
        Line line(a, b);
        while(q.size() > 0 && q.back().second >= q.back().first.intersect(line))
            q.pop_back();

        if(q.empty()) {
            q.push_back({ line, 0 });
            return ;
        }

        q.push_back({ line, q.back().first.intersect(line) });
    }

    ll query(ll x) {
        while(q.size() > 1) {
            if(q[1].second <= x) q.pop_front();
            else break;
        }

        return q.front().first.val(x);
    }
};

int n;
vector<vector<pii> > graph;
pii H[maxn];
vector<int> st;
ll dp[maxn], dist[maxn];

CHT cht;

void dfs1(int u, int p) {
    dp[u] = H[u].first + H[u].second * dist[u];

    //H[u].first + H[u].second * dist[u] - H[u].second * dist[id] + dp[id]
    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;
        dfs1(v, u);
    }

    st.pop_back();
}

void dfs2(int u, int p) {
    if(u != 1) {
        dp[u] = H[u].first + H[u].second * dist[u] + cht.query(H[u].second);
        cht.insert(-dist[u], dp[u]);
    }

    for(auto &[v, w] : graph[u]) {
        if(v == p) continue;
        dist[v] = dist[u] + w;
        dfs2(v, u);
    }
}

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;

    if(n <= 2500) {
        dfs1(1, 0);
    } else {
        cht.insert(0, 0);
        dfs2(1, 0);
    }

    for(int i=2; i<=n; i++) cout << dp[i] << " ";
    return 0;
}

Compilation message

harbingers.cpp: In function 'void dfs1(int, int)':
harbingers.cpp:78:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |     for(auto &[v, w] : graph[u]) {
      |               ^
harbingers.cpp: In function 'void dfs2(int, int)':
harbingers.cpp:93:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   93 |     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 2888 KB Output is correct
3 Incorrect 58 ms 9496 KB Output isn't correct
4 Incorrect 91 ms 12368 KB Output isn't correct
5 Incorrect 99 ms 15444 KB Output isn't correct
6 Incorrect 150 ms 18428 KB Output isn't correct
7 Incorrect 92 ms 10688 KB Output isn't correct
8 Runtime error 112 ms 24416 KB Execution killed with signal 8
9 Runtime error 116 ms 20460 KB Execution killed with signal 8
10 Runtime error 115 ms 26168 KB Execution killed with signal 8