結果
| 問題 | No.2695 Warp Zone | 
| コンテスト | |
| ユーザー |  Today03 | 
| 提出日時 | 2024-03-22 22:11:47 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 83 ms / 2,000 ms | 
| コード長 | 2,405 bytes | 
| コンパイル時間 | 1,912 ms | 
| コンパイル使用メモリ | 209,416 KB | 
| 最終ジャッジ日時 | 2025-02-20 11:44:51 | 
| ジャッジサーバーID (参考情報) | judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 24 | 
ソースコード
#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;
}
            
            
            
        