結果

問題 No.2695 Warp Zone
ユーザー akinyanakinyan
提出日時 2024-03-22 21:48:31
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 2,158 bytes
コンパイル時間 4,025 ms
コンパイル使用メモリ 265,272 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2024-03-22 21:48:36
合計ジャッジ時間 4,891 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 21 ms
19,712 KB
testcase_04 AC 21 ms
19,328 KB
testcase_05 AC 22 ms
19,328 KB
testcase_06 AC 21 ms
19,584 KB
testcase_07 AC 21 ms
19,712 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 10 ms
10,752 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 4 ms
6,676 KB
testcase_12 AC 13 ms
13,440 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 16 ms
16,000 KB
testcase_15 AC 10 ms
9,984 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 6 ms
6,784 KB
testcase_18 AC 18 ms
17,536 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 17 ms
16,640 KB
testcase_21 AC 16 ms
15,872 KB
testcase_22 AC 8 ms
7,552 KB
testcase_23 AC 14 ms
13,312 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 #

#ifndef akinyan
#define akinyan
#include <bits/stdc++.h>
using namespace std;
using ch = char;
using ll = long long;
using ld = long double;
using db = double;
using st = string;
using vdb = vector<db>;
using vvdb = vector<vdb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vs = vector<st>;
using vvs = vector<vs>;
using vc = vector<ch>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
const ll mod = 998244353;
const ll MOD = 1000000007;
const ll INF = 1000000000000000000LL;
using pall = pair<ll,ll>;
using vp = vector<pall>;
#endif

struct Edge {
    long long to;
    long long cost;
};
using Graph = vector<vector<Edge>>;
using P = pair<long, int>;

void dijkstra(const Graph &G, int s, vector<long long> &dis) {
    int N = G.size();
    dis.resize(N, INF);
    priority_queue<P, vector<P>, greater<P>> pq;  // 「仮の最短距離, 頂点」が小さい順に並ぶ
    dis[s] = 0;
    pq.emplace(dis[s], s);
    while (!pq.empty()) {
        P p = pq.top();
        pq.pop();
        int v = p.second;
        if (dis[v] < p.first) {  // 最短距離で無ければ無視
            continue;
        }
        for (auto &e : G[v]) {
            if (dis[e.to] > dis[v] + e.cost) {  // 最短距離候補なら priority_queue に追加
                dis[e.to] = dis[v] + e.cost;
                pq.emplace(dis[e.to], e.to);
            }
        }
    }
}

int main(){
    ll H,W,N;
    cin>>H>>W>>N;
    Graph G(N+2);
    vvl X(N,vl(4));
    for(int i=0; i<N; i++){
        for(int j=0; j<4; j++){
            cin>>X[i][j];
        }
    }
    for(int i=0; i<N; i++){
        G[0].push_back({i+1,abs(X[i][0] - 1) + abs(X[i][1] - 1)});
    }
    G[0].push_back({N+1,H+W-2});
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            if(i != j){
                G[i+1].push_back({j+1,abs(X[i][2] - X[j][0]) + abs(X[i][3] - X[j][1]) + 1});
            }
        }
        G[i+1].push_back({N+1,abs(H - X[i][2]) + abs(W - X[i][3]) + 1});
    }
    vl D;
    dijkstra(G,0,D);
    cout << D[N+1] << endl;
}
0