結果

問題 No.2695 Warp Zone
ユーザー Tatsu_mrTatsu_mr
提出日時 2024-03-22 22:42:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,828 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 217,076 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2024-03-22 22:42:29
合計ジャッジ時間 3,937 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 28 ms
19,712 KB
testcase_04 AC 27 ms
19,456 KB
testcase_05 AC 28 ms
19,456 KB
testcase_06 AC 29 ms
19,712 KB
testcase_07 AC 29 ms
19,712 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 13 ms
10,880 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 6 ms
6,676 KB
testcase_12 AC 17 ms
13,440 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 21 ms
16,128 KB
testcase_15 AC 12 ms
9,984 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 7 ms
6,784 KB
testcase_18 AC 23 ms
17,664 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 22 ms
16,768 KB
testcase_21 AC 20 ms
16,000 KB
testcase_22 AC 9 ms
7,552 KB
testcase_23 AC 17 ms
13,440 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<long long> dijkstra(vector<vector<pair<int, long long>>> &g, int n, int x) {
    vector<long long> dist(n, 1000000000000000000);
    vector<bool> visit(n, false);
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que;
    dist[x] = 0LL;
    que.push({dist[x], x});
    while (!que.empty()) {
        int v = que.top().second;
        que.pop();
        if (visit[v]) {
            continue;
        }
        visit[v] = true;
        for (auto p : g[v]) {
            int next_v = p.first;
            long long d = p.second;
            if (dist[next_v] > dist[v] + d) {
                dist[next_v] = dist[v] + d;
                que.push({dist[next_v], next_v});
            }
        }
    }
    return dist;
}

int main() {
    int h, w, n;
    cin >> h >> w >> n;
    vector<int> a(n), b(n), c(n), d(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    vector<vector<pair<int, long long>>> g(2 * n + 2);
    int gl = 2 * n + 1;
    g[0].push_back({gl, (long long)(h - 1 + w - 1)});
    for (int i = 0; i < n; i++) {
        int x = 2 * i + 1, y = 2 * i + 2;
        g[0].push_back({x, (long long)(a[i] - 1 + b[i] - 1)});
        g[0].push_back({y, (long long)(c[i] - 1 + d[i] - 1)});
        g[x].push_back({gl, (long long)(h - a[i] + w - b[i])});
        g[y].push_back({gl, (long long)(h - c[i] + w - d[i])});
        g[x].push_back({y, 1LL});
        for (int j = 0; j < n; j++) {
            if (i == j) {
                continue;
            }
            int x2 = 2 * j + 1;
            g[y].push_back({x2, (long long)(abs(c[i] - a[j]) + abs(d[i] - b[j]))});
        }
    }
    vector<long long> dist = dijkstra(g, 2 * n + 2, 0);
    cout << dist[2 * n + 1] << endl;
}
0