結果

問題 No.2695 Warp Zone
ユーザー Today03Today03
提出日時 2024-03-22 22:11:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,405 bytes
コンパイル時間 2,694 ms
コンパイル使用メモリ 218,256 KB
実行使用メモリ 68,096 KB
最終ジャッジ日時 2024-03-22 22:12:04
合計ジャッジ時間 4,734 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 96 ms
68,096 KB
testcase_04 AC 94 ms
66,816 KB
testcase_05 AC 92 ms
66,816 KB
testcase_06 AC 96 ms
67,840 KB
testcase_07 AC 96 ms
68,096 KB
testcase_08 AC 3 ms
6,548 KB
testcase_09 AC 36 ms
28,672 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 14 ms
12,032 KB
testcase_12 AC 52 ms
38,784 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 64 ms
48,000 KB
testcase_15 AC 34 ms
25,600 KB
testcase_16 AC 11 ms
10,112 KB
testcase_17 AC 20 ms
16,512 KB
testcase_18 AC 78 ms
58,496 KB
testcase_19 AC 2 ms
6,548 KB
testcase_20 AC 69 ms
52,480 KB
testcase_21 AC 61 ms
47,232 KB
testcase_22 AC 26 ms
19,584 KB
testcase_23 AC 50 ms
38,400 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include "./debug.cpp"
#else
#define debug(...)
#define print_line
#endif
using namespace std;
using ll = long long;

template <typename T>
vector<T> dijkstra(const vector<vector<pair<int, T>>> &G, int start = 0) {
    int n = G.size();
    vector<T> dst(n, numeric_limits<T>::max());
    priority_queue<pair<T, int>> pq;
    dst[start] = 0;
    pq.push(make_pair(0, start));
    while (!pq.empty()) {
        auto [dst_sum, now] = pq.top();
        pq.pop();
        dst_sum = -dst_sum;
        if (dst[now] < dst_sum) continue;
        for (auto [nxt, cost] : G[now]) {
            if (dst[nxt] > dst[now] + cost) {
                dst[nxt] = dst[now] + cost;
                pq.push(make_pair(-dst[nxt], nxt));
            }
        }
    }
    return dst;
}

int main() {
    int H, W, N;
    cin >> H >> W >> N;

    vector<array<int, 4>> E(N);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 4; j++) {
            cin >> E[i][j];
            E[i][j]--;
        }
    }

    vector<vector<pair<int, ll>>> G(2 * N + 2);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 4; j++) {
            G[2 * N].push_back({2 * i, E[i][0] + E[i][1]});
            G[2 * i].push_back({2 * N, E[i][0] + E[i][1]});
            G[2 * N + 1].push_back({2 * i, H - E[i][0] + W - E[i][1] - 2});
            G[2 * i].push_back({2 * N + 1, H - E[i][0] + W - E[i][1] - 2});

            G[2 * N].push_back({2 * i + 1, E[i][2] + E[i][3]});
            G[2 * i + 1].push_back({2 * N, E[i][2] + E[i][3]});
            G[2 * N + 1].push_back({2 * i + 1, H - E[i][2] + W - E[i][3] - 2});
            G[2 * i + 1].push_back({2 * N + 1, H - E[i][2] + W - E[i][3] - 2});

            G[2 * i].push_back({2 * i + 1, 1});
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 2; j++) {
            for (int i2 = 0; i2 < N; i2++) {
                for (int j2 = 0; j2 < 2; j2++) {
                    if (i == i2) continue;
                    int x1 = E[i][j * 2], y1 = E[i][j * 2 + 1], x2 = E[i2][j2 * 2], y2 = E[i2][j2 * 2 + 1];
                    G[2 * i + j].push_back({2 * i2 + j2, abs(x1 - x2) + abs(y1 - y2)});
                }
            }
        }
    }

    G[2 * N].push_back({2 * N + 1, H + W - 2});
    G[2 * N + 1].push_back({2 * N, H + W - 2});

    vector<ll> dst = dijkstra(G, 2 * N);

    cout << dst[2 * N + 1] << endl;
}
0