結果

問題 No.2695 Warp Zone
ユーザー tnakao0123tnakao0123
提出日時 2024-04-29 18:00:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,546 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 63,872 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-29 18:00:08
合計ジャッジ時間 2,012 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 17 ms
11,136 KB
testcase_04 AC 16 ms
11,008 KB
testcase_05 AC 15 ms
11,008 KB
testcase_06 AC 16 ms
11,136 KB
testcase_07 AC 17 ms
11,136 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 9 ms
7,552 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 10 ms
8,576 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 13 ms
9,600 KB
testcase_15 AC 8 ms
7,296 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 15 ms
10,240 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 13 ms
9,600 KB
testcase_21 AC 11 ms
9,600 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 10 ms
8,572 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- 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