Submission #723862

# Submission time Handle Problem Language Result Execution time Memory
723862 2023-04-14T11:32:20 Z piOOE Escape Route (JOI21_escape_route) C++17
0 / 100
9000 ms 214292 KB
#include "escape_route.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int N = 91;

ll dp[N][N * N][N], moment[N][N];
int siz[N];

void my_assert(bool f) {
    while (!f) {
        cout << "mda" << endl;
    }
}

vector<ll> calculate_necessary_time(int n, int m, ll S, int q, vector<int> A, vector<int> B, vector<ll> L, vector<ll> C,
                                    vector<int> U, vector<int> V, vector<ll> T) {
    constexpr ll inf = 3e18;
    vector<ll> answer(q, inf);
    vector<vector<int>> queries(n);

    for (int i = 0; i < q; ++i) {
        queries[U[i]].push_back(i);
    }

    vector<vector<pair<int, int>>> adj(n);

    for (int i = 0; i < m; ++i) {
        adj[A[i]].emplace_back(B[i], i);
        adj[B[i]].emplace_back(A[i], i);
    }

    auto dijkstra = [&](int source, ll s, bool rev = false) -> vector<ll> {
        vector<ll> dist(n, rev ? -inf : inf);
        dist[source] = s;
        vector<bool> used(n);

        for (int _ = 0; _ < n; ++_) {
            int v = -1;
            for (int i = 0; i < n; ++i) {
                if (!used[i] && (v == -1 || rev ^ (dist[i] < dist[v]))) {
                    v = i;
                }
            }

            used[v] = true;

            for (auto [to, i]: adj[v]) {
                ll d;
                if (rev) {
                    d = min(dist[v], C[i]) - L[i];
                } else {
                    d = L[i] + (dist[v] % S <= C[i] - L[i] ? dist[v] : (dist[v] / S + 1) * S);
                }
                if (rev ^ (dist[to] > d)) {
                    dist[to] = d;
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            dist[i] = rev ? s - dist[i] : dist[i] - s;
        }

        return dist;
    };

    vector from(n, vector<ll>(n)), dto(n, vector<ll>(n));

    for (int i = 0; i < n; ++i) {
        from[i] = dijkstra(i, 0);
    }

    for (int i = 0; i < n; ++i) {
        dto[i] = dijkstra(i, S - 1, true);
    }

    for (int i = 0; i < n; ++i) {
        for (int j: queries[i]) {
            for (int mid = 0; mid < n; ++mid) {
                ll dist = S - 1 - dto[mid][i] < T[j] ? inf : S - T[j] + from[mid][V[j]];
                answer[j] = min(answer[j], dist);
            }
        }
    }

    vector<array<ll, 4>> events; //time, idx (if query then ~), x

    vector till(n, vector<ll>(m, -inf));

    for (int i = 0; i < m; ++i) {
        int a = A[i], b = B[i];
        ll t = C[i] - L[i];

        auto da = dijkstra(a, t, true);
        auto db = dijkstra(b, t, true);

        for (int x = 0; x < n; ++x) {
            da[x] = t >= da[x] ? t - da[x] : -inf;
            db[x] = t >= db[x] ? t - db[x] : -inf;

            till[x][i] = max(da[x], db[x]);

            if (till[x][i] >= 0) {
                auto kek = dijkstra(x, till[x][i]);
                assert(till[x][i] + min(kek[a], kek[b]) <= C[i] - L[i]);

                if (till[x][i] < S - 1) {
                    ll s = till[x][i] + 1;
                    kek = dijkstra(x, s);
                    assert(s + min(kek[a], kek[b]) > C[i] - L[i]);
                }
            }
        }
    }

    for (int x = 0; x < n; ++x) {
        for (int j: queries[x]) {
            events.push_back({T[j], -inf, ~j, x});
        }
        for (int i = 0; i < m; ++i) {
            if (till[x][i] >= 0) {
                events.push_back({till[x][i], -(C[i] - L[i]), i, x});
            }
        }
    }

    sort(events.rbegin(), events.rend());

    for (int i = 0; i < n; ++i) {
        siz[i] = 1;
        fill_n(dp[i][0], n, inf);
        dp[i][0][i] = 0;
        moment[i][0] = inf;
    }

    auto distQuery = [&](int x, ll t) {
        int r = upper_bound(moment[x], moment[x] + siz[x], t, greater<>()) - moment[x];
        assert(r != 0 && (r == siz[x] || moment[x][r] < t) && moment[x][r - 1] >= t);
        return r - 1;
    };

    vector<bool> done(n);

    for (int ii = 0, jj = 0; ii < events.size();) {
        jj = ii;

        if (events[ii][2] < 0) {
            auto [ti, useless, j, x] = events[ii];
            j = ~j;
            ll d = dp[x][siz[x] - 1][V[j]];
            if (T[j] + d < S) {
                answer[j] = min(answer[j], d);
            }
            ii += 1;
            continue;
        }

        while (jj < events.size() && events[jj][0] == events[ii][0] && events[jj][2] >= 0) {
            jj += 1;
        }

        int cnt = jj - ii;
        int init = ii;

        while (cnt--) {
            ii = -1;
            ll mind = inf;

            for (int k = init; k < jj; ++k) {
                if (!done[k]) {
                    auto [ti, useless, ia, xa] = events[k];
                    ll dista = min(dp[xa][siz[xa] - 1][A[ia]], dp[xa][siz[xa] - 1][B[ia]]);
                    if (ii == -1 || mind > dista) {
                        ii = k;
                        mind = dista;
                    }
                }
            }

            my_assert(ii != -1);

            done[ii] = true;

            auto [ti, useless, j, x] = events[ii];
            if (j < 0) {
                j = ~j;
                ll d = dp[x][siz[x] - 1][V[j]];
                if (T[j] + d < S) {
                    answer[j] = min(answer[j], d);
                }
                continue;
            }

            memcpy(dp[x][siz[x]], dp[x][siz[x] - 1], sizeof(dp[x][siz[x]]));
            moment[x][siz[x]] = ti;

            int a = A[j], b = B[j];
            if (dp[x][siz[x] - 1][a] > dp[x][siz[x] - 1][b]) {
                swap(a, b);
            }

            ll distB = dp[x][siz[x] - 1][a] + L[j];
            //        cout << x << " " << j << endl;
            assert(ti + distB <= C[j]);

            if (distB < dp[x][siz[x] - 1][b]) {
                int r = distQuery(b, distB);
                for (int to = 0; to < n; ++to) {
                    dp[x][siz[x]][to] = min(dp[x][siz[x]][to], dp[b][r][to] + distB);
                }
            }

            siz[x] += 1;
        }

        ii = jj;
    }

    return answer;
}

Compilation message

escape_route.cpp: In function 'std::vector<long long int> calculate_necessary_time(int, int, ll, int, std::vector<int>, std::vector<int>, std::vector<long long int>, std::vector<long long int>, std::vector<int>, std::vector<int>, std::vector<long long int>)':
escape_route.cpp:147:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for (int ii = 0, jj = 0; ii < events.size();) {
      |                              ~~~^~~~~~~~~~~~~~~
escape_route.cpp:161:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |         while (jj < events.size() && events[jj][0] == events[ii][0] && events[jj][2] >= 0) {
      |                ~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 9020 ms 65012 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1862 ms 214292 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 9020 ms 65012 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 9020 ms 65012 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 9020 ms 65012 KB Time limit exceeded
2 Halted 0 ms 0 KB -