답안 #632414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
632414 2022-08-20T02:19:03 Z colossal_pepe Olympic Bus (JOI20_ho_t4) C++17
100 / 100
874 ms 3252 KB
#include <iostream>
#include <vector>
#include <tuple>
#include <set>
#include <cstring>
using namespace std;

using usi = unsigned int;

const usi INF = 1e8 + 1;
 
int n, m;
vector<tuple<int, int, usi, usi>> edges;
vector<vector<int>> g;
vector<vector<usi>> d;
set<int> imp_edges;
 
usi ssp(int start, int end, int flipped) {
    usi d[n];
    for (int i = 0; i < n; i++) {
        d[i] = INF;
    }
    bool vis[n];
    memset(vis, 0, sizeof vis);
    d[start] = 0;
    for (int _ = 0; _ < n; _++) {
        int u = -1;
        for (int i = 0; i < n; i++) {
            if (not vis[i] and (u == -1 or d[i] < d[u])) u = i;
        }
        for (int i : g[u]) {
            auto [a, b, w, c] = edges[i];
            if (flipped == i) swap(a, b);
            d[b] = min(d[b], d[u] + w);
        }
        vis[u] = 1;
    }
    return d[end];
}
 
void restoreEdges(int u, int start, bool vis[]) {
    vis[u] = 1;
    for (int i : g[u]) {
        auto [a, b, w, c] = edges[i];
        if (not vis[b] and d[start][b] == d[start][u] + w) {
            imp_edges.insert(i);
            restoreEdges(b, start, vis);
        }
    }
}
 
void getImpEdges() {
    bool vis[n];
    memset(vis, 0, sizeof vis);
    restoreEdges(0, 0, vis);
    memset(vis, 0, sizeof vis);
    restoreEdges(n - 1, n - 1, vis);
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    g.resize(n), edges.resize(m);
    d.resize(n, vector<usi>(n, INF));
    for (int i = 0; i < n; i++) {
        d[i][i] = 0;
    }
    for (int i = 0; i < m; i++) {
        auto &[a, b, w, c] = edges[i];
        cin >> a >> b >> w >> c;
        a--, b--;
        g[a].push_back(i);
        g[b].push_back(i);
        d[a][b] = min(d[a][b], w);
    }
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
    getImpEdges();
    // cout << "DUCK : " << endl;
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < n; j++) {
    //         cout << d[i][j] << ' ';
    //     }
    //     cout << endl;
    // }
    // cout << "FUCK : ";
    // for (int i : imp_edges) {
    //     cout << i << ' ';
    // }
    // cout << endl;
    usi ans;
    if (d[0][n - 1] == INF or d[n - 1][0] == INF) ans = 4294967295;
    else ans = d[0][n - 1] + d[n - 1][0]; // 0 0
    for (int i = 0; i < m; i++) {
        auto [a, b, w, c] = edges[i];
        if (imp_edges.find(i) != imp_edges.end()) {
            usi x = ssp(0, n - 1, i), y = ssp(n - 1, 0, i);
            if (x != INF and y != INF) ans = min(ans, x + y + c);
            continue;
        }
        if (d[0][n - 1] != INF and d[n - 1][a] != INF and d[b][0] != INF)
            ans = min(ans, d[0][n - 1] + d[n - 1][a] + w + d[b][0] + c); // 0 1
        if (d[0][n - 1] != INF and d[n - 1][b] != INF and d[a][0] != INF)
            ans = min(ans, d[0][n - 1] + d[n - 1][b] + w + d[a][0] + c); // 0 2
        if (d[0][a] != INF and d[b][n - 1] != INF and d[n - 1][0] != INF)
            ans = min(ans, d[0][a] + w + d[b][n - 1] + d[n - 1][0] + c); // 1 0
        if (d[0][a] != INF and d[b][n - 1] != INF and d[n - 1][a] != INF and d[b][0] != INF)
            ans = min(ans, d[0][a] + w + d[b][n - 1] + d[n - 1][a] + w + d[b][0] + c); // 1 1
        if (d[0][a] != INF and d[b][n - 1] != INF and d[n - 1][b] != INF and d[a][0] != INF)
            ans = min(ans, d[0][a] + w + d[b][n - 1] + d[n - 1][b] + w + d[a][0] + c); // 1 2
        if (d[0][b] != INF and d[a][n - 1] != INF and d[n - 1][0] != INF)
            ans = min(ans, d[0][b] + w + d[a][n - 1] + d[n - 1][0] + c); // 2 0
        if (d[0][b] != INF and d[a][n - 1] != INF and d[n - 1][a] != INF and d[b][0] != INF)
            ans = min(ans, d[0][b] + w + d[a][n - 1] + d[n - 1][a] + w + d[b][0] + c); // 2 1
        if (d[0][b] != INF and d[a][n - 1] != INF and d[n - 1][b] != INF and d[a][0] != INF)
            ans = min(ans, d[0][b] + w + d[a][n - 1] + d[n - 1][b] + w + d[a][0] + c); // 2 2
    }
    if (ans == 4294967295) cout << -1 << '\n';
    else cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 588 KB Output is correct
2 Correct 9 ms 468 KB Output is correct
3 Correct 65 ms 556 KB Output is correct
4 Correct 64 ms 544 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 10 ms 468 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 48 ms 468 KB Output is correct
11 Correct 63 ms 560 KB Output is correct
12 Correct 62 ms 564 KB Output is correct
13 Correct 33 ms 468 KB Output is correct
14 Correct 53 ms 560 KB Output is correct
15 Correct 50 ms 536 KB Output is correct
16 Correct 45 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 666 ms 2480 KB Output is correct
2 Correct 731 ms 2468 KB Output is correct
3 Correct 684 ms 2456 KB Output is correct
4 Correct 65 ms 584 KB Output is correct
5 Correct 49 ms 468 KB Output is correct
6 Correct 9 ms 520 KB Output is correct
7 Correct 13 ms 468 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 356 ms 2388 KB Output is correct
10 Correct 356 ms 2476 KB Output is correct
11 Correct 652 ms 2480 KB Output is correct
12 Correct 638 ms 2468 KB Output is correct
13 Correct 530 ms 2500 KB Output is correct
14 Correct 493 ms 2568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 552 KB Output is correct
2 Correct 23 ms 456 KB Output is correct
3 Correct 389 ms 2120 KB Output is correct
4 Correct 15 ms 468 KB Output is correct
5 Correct 645 ms 2460 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 461 ms 2844 KB Output is correct
9 Correct 418 ms 2840 KB Output is correct
10 Correct 732 ms 2848 KB Output is correct
11 Correct 663 ms 2840 KB Output is correct
12 Correct 704 ms 3012 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 718 ms 2920 KB Output is correct
20 Correct 716 ms 2944 KB Output is correct
21 Correct 666 ms 2864 KB Output is correct
22 Correct 721 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 588 KB Output is correct
2 Correct 9 ms 468 KB Output is correct
3 Correct 65 ms 556 KB Output is correct
4 Correct 64 ms 544 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 10 ms 468 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 48 ms 468 KB Output is correct
11 Correct 63 ms 560 KB Output is correct
12 Correct 62 ms 564 KB Output is correct
13 Correct 33 ms 468 KB Output is correct
14 Correct 53 ms 560 KB Output is correct
15 Correct 50 ms 536 KB Output is correct
16 Correct 45 ms 468 KB Output is correct
17 Correct 666 ms 2480 KB Output is correct
18 Correct 731 ms 2468 KB Output is correct
19 Correct 684 ms 2456 KB Output is correct
20 Correct 65 ms 584 KB Output is correct
21 Correct 49 ms 468 KB Output is correct
22 Correct 9 ms 520 KB Output is correct
23 Correct 13 ms 468 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 356 ms 2388 KB Output is correct
26 Correct 356 ms 2476 KB Output is correct
27 Correct 652 ms 2480 KB Output is correct
28 Correct 638 ms 2468 KB Output is correct
29 Correct 530 ms 2500 KB Output is correct
30 Correct 493 ms 2568 KB Output is correct
31 Correct 60 ms 552 KB Output is correct
32 Correct 23 ms 456 KB Output is correct
33 Correct 389 ms 2120 KB Output is correct
34 Correct 15 ms 468 KB Output is correct
35 Correct 645 ms 2460 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 461 ms 2844 KB Output is correct
39 Correct 418 ms 2840 KB Output is correct
40 Correct 732 ms 2848 KB Output is correct
41 Correct 663 ms 2840 KB Output is correct
42 Correct 704 ms 3012 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 320 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 718 ms 2920 KB Output is correct
50 Correct 716 ms 2944 KB Output is correct
51 Correct 666 ms 2864 KB Output is correct
52 Correct 721 ms 2892 KB Output is correct
53 Correct 816 ms 2764 KB Output is correct
54 Correct 795 ms 2816 KB Output is correct
55 Correct 874 ms 2888 KB Output is correct
56 Correct 64 ms 468 KB Output is correct
57 Correct 54 ms 556 KB Output is correct
58 Correct 477 ms 2432 KB Output is correct
59 Correct 547 ms 2424 KB Output is correct
60 Correct 718 ms 2556 KB Output is correct
61 Correct 464 ms 2428 KB Output is correct
62 Correct 542 ms 2436 KB Output is correct
63 Correct 739 ms 2428 KB Output is correct
64 Correct 458 ms 2428 KB Output is correct
65 Correct 519 ms 2436 KB Output is correct
66 Correct 714 ms 2432 KB Output is correct
67 Correct 13 ms 2256 KB Output is correct
68 Correct 461 ms 3068 KB Output is correct
69 Correct 459 ms 3148 KB Output is correct
70 Correct 694 ms 3092 KB Output is correct
71 Correct 727 ms 3060 KB Output is correct
72 Correct 745 ms 3028 KB Output is correct
73 Correct 766 ms 3252 KB Output is correct
74 Correct 746 ms 3148 KB Output is correct