結果

問題 No.2695 Warp Zone
ユーザー akinyanakinyan
提出日時 2024-03-22 21:48:31
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,158 bytes
コンパイル時間 3,264 ms
コンパイル使用メモリ 264,040 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2024-09-30 11:16:57
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

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