結果

問題 No.2695 Warp Zone
ユーザー Nichi10pNichi10p
提出日時 2024-03-22 23:40:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,266 bytes
コンパイル時間 2,344 ms
コンパイル使用メモリ 212,360 KB
実行使用メモリ 35,712 KB
最終ジャッジ日時 2024-03-22 23:40:05
合計ジャッジ時間 3,882 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 61 ms
35,712 KB
testcase_04 AC 59 ms
35,072 KB
testcase_05 AC 59 ms
35,072 KB
testcase_06 AC 60 ms
35,584 KB
testcase_07 AC 61 ms
35,712 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 25 ms
18,048 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 9 ms
8,320 KB
testcase_12 AC 35 ms
23,680 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 42 ms
28,416 KB
testcase_15 AC 23 ms
16,384 KB
testcase_16 AC 9 ms
7,680 KB
testcase_17 AC 13 ms
9,856 KB
testcase_18 AC 49 ms
31,488 KB
testcase_19 AC 2 ms
6,548 KB
testcase_20 AC 45 ms
29,696 KB
testcase_21 AC 42 ms
28,160 KB
testcase_22 AC 18 ms
11,392 KB
testcase_23 AC 34 ms
23,296 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using pii = pair<int, int>;

template<class T> bool chmin(T &a, const T &x) { return x < a ? a = x, true : false; }

int md(pii const &p1, pii const &p2) {
  return abs(p1.first - p2.first) + abs(p1.second - p2.second);
}

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  vector<pii> P(N+N);
  for (int i = 0; i < N; ++i)
    cin >> P[i].first >> P[i].second >> P[N+i].first >> P[N+i].second;

  int const S=N+N, T=N+N+1;
  vector<vector<pii>> G(N+N+2);  // cost, to_id
  G[S].emplace_back(md({1, 1}, {H, W}), T);
  for (int i = 0; i < N+N; ++i) {
    G[S].emplace_back(md({1, 1}, P[i]), i);
    G[i].emplace_back(md(P[i], {H, W}), T);
  }
  for (int i = 0; i < N+N; ++i)
  for (int j = 0; j < N+N; ++j)
  if (i != j) {
    if (N+i == j)
      G[i].emplace_back(1, j);
    else
      G[i].emplace_back(md(P[i], P[j]), j);
  }

  vector<int> answer(N+N+2, 2e9+9);
  priority_queue<pii, vector<pii>, greater<void>> pq;  // distance, id
  answer[S] = 0;
  pq.emplace(0, S);
  while (!pq.empty()) {
    auto const [d, u] = pq.top();
    pq.pop();
    if (d > answer[u])
      continue;
    for (auto const &[c, v] : G[u])
      if (chmin(answer[v], d + c))
        pq.emplace(d+c, v);
  }

  cout << answer[T] << endl;
}
0