結果

問題 No.2695 Warp Zone
ユーザー haihamabossu
提出日時 2024-04-16 22:34:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,217 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 203,576 KB
最終ジャッジ日時 2025-02-21 02:42:02
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void solve() {
  ll h, w, n;
  cin >> h >> w >> n;
  vector<pair<ll, ll>> vs;
  vs.reserve(n * 2 + 2);
  rep(i, n) rep(_, 2) {
    ll a, b;
    cin >> a >> b;
    vs.emplace_back(a, b);
  }
  vs.emplace_back(1, 1);
  vs.emplace_back(h, w);
  auto calc_dist = [&](ll i, ll j) -> ll {
    if (i < n * 2 && i % 2 == 0 && i + 1 == j) {
      return 1;
    }
    ll dx = vs[i].first - vs[j].first;
    ll dy = vs[i].second - vs[j].second;
    return abs(dx) + abs(dy);
  };
  priority_queue<pair<ll, ll>> que;
  vector<ll> dist(n * 2 + 2, 1e18);
  que.emplace(0, n * 2);
  dist[n * 2] = 0;
  while (!que.empty()) {
    auto [cd, i] = que.top();
    que.pop();
    cd = -cd;
    if (dist[i] < cd)
      continue;
    rep(j, vs.size()) {
      if (i == j)
        continue;
      ll nd = cd + calc_dist(i, j);
      if (dist[j] <= nd)
        continue;
      dist[j] = nd;
      que.emplace(-nd, j);
    }
  }
  cout << dist[n * 2 + 1] << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0