結果

問題 No.2695 Warp Zone
ユーザー askr58
提出日時 2024-03-22 22:34:41
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,693 bytes
コンパイル時間 5,751 ms
コンパイル使用メモリ 319,008 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-09-30 12:01:12
合計ジャッジ時間 6,334 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
ll inf=1000000000000000001;
struct warp{
    ll x1,x2,y1,y2;
    warp(){}
    warp(ll x1,ll y1,ll x2,ll y2):x1(x1),y1(y1),x2(x2),y2(y2){};
};
struct edge{
    int from,to;
    ll w;
    edge(){}
    edge(int f,int t,ll w):from(f),to(t),w(w){}
};
int main()
{
    ll h,w;
    int n;
    cin>>h>>w>>n;
    vector<warp> warps(n);
    for(int i=0;i<n;i++){
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        warps[i]=warp(a,b,c,d);
    }
    vector<vector<edge>> graph(2*n+2);
    auto f=[](ll x1,ll y1,ll x2,ll y2)->ll{
        return abs(x2-x1)+abs(y2-y1);
    };
    graph[0].push_back(edge(0,2*n+1,h+w-2));
    for(int i=0;i<n;i++){
        graph[0].push_back(edge(0,i+1,f(1,1,warps[i].x1,warps[i].y1)));
        graph[n+1+i].push_back(edge(n+1+i,2*n+1,f(warps[i].x2,warps[i].y2,h,w)));
    }
    for(int i=0;i<n;i++){
        graph[i+1].push_back(edge(i+1,n+i+1,1));
        for(int j=0;j<n;j++){
            if(j==i)continue;
            graph[n+i+1].push_back(edge(n+i+1,j+1,f(warps[i].x2,warps[i].y2,warps[j].x1,warps[j].y1)));
        }
    }

    vector<ll> distance(2*n+2,inf);
    using pli=pair<ll,int>;
    priority_queue<pli,vector<pli>,greater<pli>> pq;
    pq.push(make_pair(0,0));
    while(!pq.empty()){
        auto [c,p]=pq.top();
        pq.pop();
        if(distance[p]<c)continue;
        else distance[p]=c;
        for(edge e:graph[p]){
            if(distance[e.to]<=c+e.w)continue;
            else{
                distance[e.to]=c+e.w;
                pq.push(make_pair(c+e.w,e.to));
            }
        }
    }
    cout<<distance[2*n+1]<<endl;



}
0