結果

問題 No.2695 Warp Zone
ユーザー tnakao0123
提出日時 2024-04-29 18:00:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,546 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 65,024 KB
最終ジャッジ日時 2025-02-21 09:40:20
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:43:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |   scanf("%d%d%d", &h, &w, &n);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:46:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |     scanf("%d%d%d%d", xs + i, ys + i, xs + n + i, ys + n + i);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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