답안 #390787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390787 2021-04-16T22:49:00 Z timmyfeng Olympic Bus (JOI20_ho_t4) C++17
100 / 100
847 ms 3476 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 201, M = 50000;

int dist[N][N], depth[N], prv[N], n, m;
bool path_1[M], path_n[M], path[M];
vector<array<int, 3>> adj[N];
array<int, 4> edges[M];

int dijkstra(int s, int t, bool *path) {
    fill(adj, adj + n + 1, vector<array<int, 3>>());
    fill(depth, depth + n + 1, INT_MAX);

    for (int j = 0; j < m; ++j) {
        auto [u, v, c, d] = edges[j];
        adj[u].push_back({v, c, j});
    }

    priority_queue<array<int, 2>> queue;
    queue.push({0, s});
    depth[0] = 0;

    while (!queue.empty()) {
        auto [d, u] = queue.top();
        queue.pop();
        d = -d;

        if (d > depth[u]) {
            continue;
        }

        for (auto [c, w, i] : adj[u]) {
            if (d + w < depth[c]) {
                prv[c] = i;
                depth[c] = d + w;
                queue.push({-depth[c], c});
            }
        }
    }

    if (depth[t] < INT_MAX) {
        int i = t;
        while (i != s) {
            auto [u, v, c, d] = edges[prv[i]];
            path[prv[i]] = true;
            i = u;
        }
    }

    return depth[t];
}

int calc(int s, int t, int i) {
    auto [u, v, c, d] = edges[i];
    if (max(dist[s][u], dist[v][t]) < INT_MAX) {
        return min(dist[s][t], dist[s][u] + dist[v][t] + c);
    } else {
        return dist[s][t];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            dist[i][j] = i == j ? 0 : INT_MAX;
        }
    }

    for (int i = 0; i < m; ++i) {
        auto &[u, v, c, d] = edges[i];
        cin >> u >> v >> c >> d;
        dist[u][v] = min(dist[u][v], c);
    }

    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (max(dist[i][k], dist[k][j]) < INT_MAX) {
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
    }

    dijkstra(1, n, path_1);
    dijkstra(n, 1, path_n);

    int ans = max(dist[1][n], dist[n][1]) < INT_MAX ? dist[1][n] + dist[n][1] : INT_MAX;

    for (int i = 0; i < m; ++i) {
        auto &[u, v, c, d] = edges[i];
        swap(u, v);
        int dist_1 = path_1[i] ? dijkstra(1, n, path) : calc(1, n, i);
        int dist_n = path_n[i] ? dijkstra(n, 1, path) : calc(n, 1, i);
        if (max(dist_1, dist_n) < INT_MAX) {
            ans = min(ans, dist_1 + dist_n + d);
        }
        swap(u, v);
    }

    cout << (ans < INT_MAX ? ans : -1) << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 508 KB Output is correct
2 Correct 13 ms 496 KB Output is correct
3 Correct 15 ms 460 KB Output is correct
4 Correct 15 ms 460 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 14 ms 588 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 35 ms 460 KB Output is correct
11 Correct 34 ms 460 KB Output is correct
12 Correct 35 ms 472 KB Output is correct
13 Correct 15 ms 536 KB Output is correct
14 Correct 15 ms 536 KB Output is correct
15 Correct 15 ms 532 KB Output is correct
16 Correct 15 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 2272 KB Output is correct
2 Correct 40 ms 2208 KB Output is correct
3 Correct 43 ms 2288 KB Output is correct
4 Correct 15 ms 460 KB Output is correct
5 Correct 15 ms 508 KB Output is correct
6 Correct 14 ms 460 KB Output is correct
7 Correct 13 ms 492 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 37 ms 2140 KB Output is correct
10 Correct 36 ms 2132 KB Output is correct
11 Correct 45 ms 2244 KB Output is correct
12 Correct 46 ms 2252 KB Output is correct
13 Correct 45 ms 2276 KB Output is correct
14 Correct 43 ms 2372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 472 KB Output is correct
2 Correct 13 ms 508 KB Output is correct
3 Correct 31 ms 1728 KB Output is correct
4 Correct 13 ms 508 KB Output is correct
5 Correct 41 ms 2212 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 35 ms 2284 KB Output is correct
9 Correct 35 ms 3156 KB Output is correct
10 Correct 38 ms 3212 KB Output is correct
11 Correct 37 ms 3132 KB Output is correct
12 Correct 38 ms 3140 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 38 ms 3172 KB Output is correct
20 Correct 39 ms 3312 KB Output is correct
21 Correct 38 ms 3076 KB Output is correct
22 Correct 38 ms 3104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 508 KB Output is correct
2 Correct 13 ms 496 KB Output is correct
3 Correct 15 ms 460 KB Output is correct
4 Correct 15 ms 460 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 14 ms 588 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 35 ms 460 KB Output is correct
11 Correct 34 ms 460 KB Output is correct
12 Correct 35 ms 472 KB Output is correct
13 Correct 15 ms 536 KB Output is correct
14 Correct 15 ms 536 KB Output is correct
15 Correct 15 ms 532 KB Output is correct
16 Correct 15 ms 460 KB Output is correct
17 Correct 40 ms 2272 KB Output is correct
18 Correct 40 ms 2208 KB Output is correct
19 Correct 43 ms 2288 KB Output is correct
20 Correct 15 ms 460 KB Output is correct
21 Correct 15 ms 508 KB Output is correct
22 Correct 14 ms 460 KB Output is correct
23 Correct 13 ms 492 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 37 ms 2140 KB Output is correct
26 Correct 36 ms 2132 KB Output is correct
27 Correct 45 ms 2244 KB Output is correct
28 Correct 46 ms 2252 KB Output is correct
29 Correct 45 ms 2276 KB Output is correct
30 Correct 43 ms 2372 KB Output is correct
31 Correct 15 ms 472 KB Output is correct
32 Correct 13 ms 508 KB Output is correct
33 Correct 31 ms 1728 KB Output is correct
34 Correct 13 ms 508 KB Output is correct
35 Correct 41 ms 2212 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 35 ms 2284 KB Output is correct
39 Correct 35 ms 3156 KB Output is correct
40 Correct 38 ms 3212 KB Output is correct
41 Correct 37 ms 3132 KB Output is correct
42 Correct 38 ms 3140 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 328 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 38 ms 3172 KB Output is correct
50 Correct 39 ms 3312 KB Output is correct
51 Correct 38 ms 3076 KB Output is correct
52 Correct 38 ms 3104 KB Output is correct
53 Correct 39 ms 3228 KB Output is correct
54 Correct 44 ms 3268 KB Output is correct
55 Correct 43 ms 3220 KB Output is correct
56 Correct 15 ms 568 KB Output is correct
57 Correct 15 ms 476 KB Output is correct
58 Correct 284 ms 2772 KB Output is correct
59 Correct 284 ms 2768 KB Output is correct
60 Correct 291 ms 2756 KB Output is correct
61 Correct 214 ms 2716 KB Output is correct
62 Correct 262 ms 2888 KB Output is correct
63 Correct 265 ms 2692 KB Output is correct
64 Correct 847 ms 3200 KB Output is correct
65 Correct 782 ms 3068 KB Output is correct
66 Correct 767 ms 3328 KB Output is correct
67 Correct 18 ms 2632 KB Output is correct
68 Correct 37 ms 3392 KB Output is correct
69 Correct 37 ms 3324 KB Output is correct
70 Correct 46 ms 3408 KB Output is correct
71 Correct 43 ms 3412 KB Output is correct
72 Correct 43 ms 3396 KB Output is correct
73 Correct 42 ms 3444 KB Output is correct
74 Correct 45 ms 3476 KB Output is correct