結果

問題 No.2695 Warp Zone
ユーザー umezoumezo
提出日時 2024-03-22 22:42:50
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,436 bytes
コンパイル時間 3,485 ms
コンパイル使用メモリ 260,648 KB
実行使用メモリ 35,840 KB
最終ジャッジ日時 2024-03-22 22:42:55
合計ジャッジ時間 4,440 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 38 ms
35,840 KB
testcase_04 AC 40 ms
35,200 KB
testcase_05 AC 39 ms
35,200 KB
testcase_06 AC 38 ms
35,584 KB
testcase_07 AC 38 ms
35,840 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 17 ms
18,048 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 7 ms
8,320 KB
testcase_12 AC 23 ms
23,552 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 29 ms
28,544 KB
testcase_15 AC 17 ms
16,512 KB
testcase_16 AC 6 ms
7,680 KB
testcase_17 AC 9 ms
9,856 KB
testcase_18 AC 32 ms
31,488 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 31 ms
29,824 KB
testcase_21 AC 30 ms
28,288 KB
testcase_22 AC 11 ms
11,520 KB
testcase_23 AC 23 ms
23,296 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 #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll h,w,n;
  cin>>h>>w>>n;
  vector<ll> A(n),B(n),C(n),D(n);
  rep(i,n){
    cin>>A[i]>>B[i]>>C[i]>>D[i];
    A[i]--,B[i]--,C[i]--,D[i]--;
  }
  
  Graph G(2*n+2);
  rep(i,n){
    G[i].push_back(Edge(i+n,1));
  }
  G[2*n].push_back(Edge(2*n+1,h+w-2));
  rep(i,n){
     G[2*n].push_back(Edge(i,A[i]+B[i]));
     G[i+n].push_back(Edge(2*n+1,h+w-2-C[i]-D[i]));
  }
  rep(i,n){
    rep(j,n){
      G[i].push_back(Edge(j,abs(A[i]-A[j])+abs(B[i]-B[j])));
      G[i+n].push_back(Edge(j,abs(C[i]-A[j])+abs(D[i]-B[j])));
    }
  }
  
  vector<ll> dist(2*n+2,INF);
  int s=2*n;
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  cout<<dist[2*n+1]<<endl;
  
  return 0;
}
0