結果

問題 No.2695 Warp Zone
コンテスト
ユーザー tnakao0123
提出日時 2024-04-29 18:00:06
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,546 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 405 ms
コンパイル使用メモリ 80,848 KB
実行使用メモリ 11,904 KB
最終ジャッジ日時 2026-07-04 15:51:24
合計ジャッジ時間 1,777 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 2695.cc:  No.2695 Warp Zone - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int MAX_GN = MAX_N * 2 + 2;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;

/* global variables */

int xs[MAX_GN], ys[MAX_GN];
vpii nbrs[MAX_GN];
ll ds[MAX_GN];

/* subroutines */

inline int dist(int i, int j) {
  return abs(xs[j] - xs[i]) + abs(ys[j] - ys[i]);
}

/* main */

int main() {
  int h, w, n;
  scanf("%d%d%d", &h, &w, &n);

  for (int i = 0; i < n; i++)
    scanf("%d%d%d%d", xs + i, ys + i, xs + n + i, ys + n + i);

  int st = n * 2, gl = st + 1, gn = gl + 1;
  xs[st] = 1, ys[st] = 1;
  xs[gl] = h, ys[gl] = w;
  nbrs[st].push_back({gl, dist(st, gl)});

  for (int i = 0; i < n; i++) {
    nbrs[st].push_back({i, dist(st, i)});
    nbrs[n + i].push_back({gl, dist(n + i, gl)});
    nbrs[i].push_back({n + i, 1});
    for (int j = 0; j < n; j++)
      nbrs[n + i].push_back({j, dist(n + i, j)});
  }

  fill(ds, ds + gn, LINF);
  ds[st] = 0;
  priority_queue<pli> q;
  q.push({0, st});

  while (! q.empty()) {
    auto [ud, u] = q.top(); q.pop();
    ud = -ud;
    if (ds[u] != ud) continue;
    if (u == gl) break;

    for (auto [v, w]: nbrs[u]) {
      ll vd = ud + w;
      if (ds[v] > vd) {
	ds[v] = vd;
	q.push({-vd, v});
      }
    }
  }

  printf("%lld\n", ds[gl]);

  return 0;
}
0