답안 #901957

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
901957 2024-01-10T04:51:01 Z box Escape Route (JOI21_escape_route) C++17
100 / 100
2269 ms 201896 KB
// #pragma GCC optimize("Ofast")
#ifndef LOCAL
#include "escape_route.h"
#endif
#include <bits/stdc++.h>
using namespace std;
 
#ifdef LOCAL
#define bug(x) cerr << "L" << __LINE__ << " | " << #x << " => " << x << endl
#else
#define bug(x)
#define cerr if (0) cerr
#endif
 
#define ar array
#define sz(v) int(std::size(v))
#define all(v) std::begin(v), std::end(v)
using i64 = long long;
using vi = vector<int>;
using pi = pair<int, int>;
 
vector<i64> calculate_necessary_time(int N, int M, i64 S, int Q, vi A, vi B, vector<i64> L, vector<i64> C, vi U, vi V, vector<i64> T)
{
    const int MAXN = 90, MAXM = MAXN*MAXN;
    const i64 INF = 1e18;
    static vi adj[MAXN];
    for (int i = 0; i < M; i++) adj[A[i]].push_back(i), adj[B[i]].push_back(i);
    static i64 dist[MAXN][MAXN], end[MAXN][MAXN];
    for (int src = 0; src < N; src++) {
        {
            fill(dist[src], dist[src] + N, INF);
            using X = pair<i64, int>;
            priority_queue<X, vector<X>, greater<X>> pq;
            auto ins = [&](int i, i64 d) {
                if (dist[src][i] > d)
                    pq.push({dist[src][i] = d, i});
            };
            ins(src, 0);
            while (sz(pq)) {
                auto [d, i] = pq.top(); pq.pop();
                if (dist[src][i] != d) continue;
                for (int h : adj[i]) ins(i ^ A[h] ^ B[h], L[h] + (d % S <= C[h] - L[h] ? d : (d / S + 1) * S));
            }
        }
        {
            fill(end[src], end[src] + N, -1);
            using X = pair<i64, int>;
            priority_queue<X> pq;
            auto ins = [&](int i, i64 d) {
                if (end[src][i] < d)
                    pq.push({end[src][i] = d, i});
            };
            ins(src, S);
            while (sz(pq)) {
                auto [d, i] = pq.top(); pq.pop();
                if (end[src][i] != d) continue;
                for (int h : adj[i]) ins(i ^ A[h] ^ B[h], min(C[h], d) - L[h]);
            }
        }
 
    }
    vector<i64> ans(Q, INF);
    for (int i = 0; i < Q; i++) for (int j = 0; j < N; j++) if (T[i] <= end[j][U[i]]) ans[i] = min(ans[i], S - T[i] + dist[j][V[i]]);
    static i64 one[MAXM][MAXN], two[MAXM][MAXN], mid[MAXM]; int E = 0;
    static bool vis[MAXN];
    vector<tuple<i64, int, int>> upd;
    for (int I = 0; I < N; I++) for (int H : adj[I]) {
        int J = I^A[H]^B[H];
        mid[E] = L[H];
        {
            fill(one[E], one[E]+N, INF);
            fill(vis, vis+N, 0);
            one[E][I] = 0;
            for (int rep = 0; rep < N; rep++) {
                int i = -1;
                for (int j = 0; j < N; j++) if (!vis[j] && one[E][j] < INF && (i == -1 || one[E][i] > one[E][j])) i = j;
                if (i == -1) break;
                vis[i] = 1;
                for (int h : adj[i]) {
                    int j = i^A[h]^B[h];
                    i64 T = C[H]-L[H]-one[E][i];
                    if (T <= C[h]) one[E][j] = min(one[E][j], one[E][i]+L[h]);
                }
            }
        }
        {
            fill(two[E], two[E]+N, INF);
            fill(vis, vis+N, 0);
            two[E][J] = 0;
            for (int rep = 0; rep < N; rep++) {
                int i = -1;
                for (int j = 0; j < N; j++) if (!vis[j] && two[E][j] < INF && (i == -1 || two[E][i] > two[E][j])) i = j;
                if (i == -1) break;
                vis[i] = 1;
                for (int h : adj[i]) {
                    int j = i^A[h]^B[h];
                    i64 T = two[E][i]+C[H];
                    if (T <= C[h]-L[h]) two[E][j] = min(two[E][j], two[E][i]+L[h]);
                }
            }
        }
        for (int x = 0; x < N; x++) if (one[E][x] < INF) {
            i64 T = C[H]-L[H]-one[E][x];
            // assert(T >= 0);
            upd.push_back({T, E, x});
        }
        E++;
    }
    vi perm(Q);
    iota(all(perm), 0);
    sort(all(perm), [&](int i, int j) {
        return T[i]>T[j];
    });
    sort(all(upd), greater<>());
    int p = 0;
    static i64 other[MAXN][MAXN];
    for (int i = 0; i < N; i++) fill(other[i], other[i]+N, INF);
    for (int i : perm) {
        while (p < sz(upd) && get<0>(upd[p]) >= T[i]) {
            auto [T, E, x] = upd[p++];
            for (int y = 0; y < N; y++) other[x][y] = min(other[x][y], one[E][x]+two[E][y]+mid[E]);
        }
        ans[i] = min(ans[i], other[U[i]][V[i]]);
    }
    return ans;
}

#ifdef LOCAL
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int N, M, Q; i64 S; cin >> N >> M >> S >> Q;
    vi A(M), B(M); vector<i64> L(M), C(M);
    for (int i = 0; i < M; i++) cin >> A[i] >> B[i] >> L[i] >> C[i];
    vi U(Q), V(Q); vector<i64> T(Q);
    for (int i = 0; i < Q; i++) cin >> U[i] >> V[i] >> T[i];
    auto ans = calculate_necessary_time(N, M, S, Q, A, B, L, C, U, V, T);
    assert(sz(ans) == Q);
    for (int i = 0; i < Q; i++) cout << ans[i] << '\n';
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 65116 KB Output is correct
2 Correct 12 ms 65116 KB Output is correct
3 Correct 53 ms 65116 KB Output is correct
4 Correct 10 ms 65116 KB Output is correct
5 Correct 27 ms 65116 KB Output is correct
6 Correct 10 ms 65204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 913 ms 112300 KB Output is correct
2 Correct 932 ms 178676 KB Output is correct
3 Correct 901 ms 159992 KB Output is correct
4 Correct 978 ms 188256 KB Output is correct
5 Correct 952 ms 188736 KB Output is correct
6 Correct 8 ms 65116 KB Output is correct
7 Correct 912 ms 159276 KB Output is correct
8 Correct 811 ms 198840 KB Output is correct
9 Correct 852 ms 155212 KB Output is correct
10 Correct 985 ms 188180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 65116 KB Output is correct
2 Correct 12 ms 65116 KB Output is correct
3 Correct 53 ms 65116 KB Output is correct
4 Correct 10 ms 65116 KB Output is correct
5 Correct 27 ms 65116 KB Output is correct
6 Correct 10 ms 65204 KB Output is correct
7 Correct 913 ms 112300 KB Output is correct
8 Correct 932 ms 178676 KB Output is correct
9 Correct 901 ms 159992 KB Output is correct
10 Correct 978 ms 188256 KB Output is correct
11 Correct 952 ms 188736 KB Output is correct
12 Correct 8 ms 65116 KB Output is correct
13 Correct 912 ms 159276 KB Output is correct
14 Correct 811 ms 198840 KB Output is correct
15 Correct 852 ms 155212 KB Output is correct
16 Correct 985 ms 188180 KB Output is correct
17 Correct 967 ms 158180 KB Output is correct
18 Correct 916 ms 157520 KB Output is correct
19 Correct 964 ms 181644 KB Output is correct
20 Correct 948 ms 161904 KB Output is correct
21 Correct 987 ms 190816 KB Output is correct
22 Correct 959 ms 190820 KB Output is correct
23 Correct 1071 ms 161168 KB Output is correct
24 Correct 963 ms 200668 KB Output is correct
25 Correct 911 ms 157608 KB Output is correct
26 Correct 995 ms 190880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 65116 KB Output is correct
2 Correct 12 ms 65116 KB Output is correct
3 Correct 53 ms 65116 KB Output is correct
4 Correct 10 ms 65116 KB Output is correct
5 Correct 27 ms 65116 KB Output is correct
6 Correct 10 ms 65204 KB Output is correct
7 Correct 913 ms 112300 KB Output is correct
8 Correct 932 ms 178676 KB Output is correct
9 Correct 901 ms 159992 KB Output is correct
10 Correct 978 ms 188256 KB Output is correct
11 Correct 952 ms 188736 KB Output is correct
12 Correct 8 ms 65116 KB Output is correct
13 Correct 912 ms 159276 KB Output is correct
14 Correct 811 ms 198840 KB Output is correct
15 Correct 852 ms 155212 KB Output is correct
16 Correct 985 ms 188180 KB Output is correct
17 Correct 967 ms 158180 KB Output is correct
18 Correct 916 ms 157520 KB Output is correct
19 Correct 964 ms 181644 KB Output is correct
20 Correct 948 ms 161904 KB Output is correct
21 Correct 987 ms 190816 KB Output is correct
22 Correct 959 ms 190820 KB Output is correct
23 Correct 1071 ms 161168 KB Output is correct
24 Correct 963 ms 200668 KB Output is correct
25 Correct 911 ms 157608 KB Output is correct
26 Correct 995 ms 190880 KB Output is correct
27 Correct 1166 ms 160692 KB Output is correct
28 Correct 1209 ms 160136 KB Output is correct
29 Correct 1306 ms 183224 KB Output is correct
30 Correct 1230 ms 164320 KB Output is correct
31 Correct 1261 ms 192804 KB Output is correct
32 Correct 1237 ms 192940 KB Output is correct
33 Correct 968 ms 201236 KB Output is correct
34 Correct 1175 ms 157984 KB Output is correct
35 Correct 1260 ms 193280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 65116 KB Output is correct
2 Correct 12 ms 65116 KB Output is correct
3 Correct 53 ms 65116 KB Output is correct
4 Correct 10 ms 65116 KB Output is correct
5 Correct 27 ms 65116 KB Output is correct
6 Correct 10 ms 65204 KB Output is correct
7 Correct 913 ms 112300 KB Output is correct
8 Correct 932 ms 178676 KB Output is correct
9 Correct 901 ms 159992 KB Output is correct
10 Correct 978 ms 188256 KB Output is correct
11 Correct 952 ms 188736 KB Output is correct
12 Correct 8 ms 65116 KB Output is correct
13 Correct 912 ms 159276 KB Output is correct
14 Correct 811 ms 198840 KB Output is correct
15 Correct 852 ms 155212 KB Output is correct
16 Correct 985 ms 188180 KB Output is correct
17 Correct 967 ms 158180 KB Output is correct
18 Correct 916 ms 157520 KB Output is correct
19 Correct 964 ms 181644 KB Output is correct
20 Correct 948 ms 161904 KB Output is correct
21 Correct 987 ms 190816 KB Output is correct
22 Correct 959 ms 190820 KB Output is correct
23 Correct 1071 ms 161168 KB Output is correct
24 Correct 963 ms 200668 KB Output is correct
25 Correct 911 ms 157608 KB Output is correct
26 Correct 995 ms 190880 KB Output is correct
27 Correct 1166 ms 160692 KB Output is correct
28 Correct 1209 ms 160136 KB Output is correct
29 Correct 1306 ms 183224 KB Output is correct
30 Correct 1230 ms 164320 KB Output is correct
31 Correct 1261 ms 192804 KB Output is correct
32 Correct 1237 ms 192940 KB Output is correct
33 Correct 968 ms 201236 KB Output is correct
34 Correct 1175 ms 157984 KB Output is correct
35 Correct 1260 ms 193280 KB Output is correct
36 Correct 2240 ms 164884 KB Output is correct
37 Correct 2236 ms 163608 KB Output is correct
38 Correct 2260 ms 167760 KB Output is correct
39 Correct 2269 ms 196044 KB Output is correct
40 Correct 2229 ms 196076 KB Output is correct
41 Correct 1008 ms 201896 KB Output is correct
42 Correct 2243 ms 158088 KB Output is correct
43 Correct 2235 ms 158900 KB Output is correct