結果
問題 | No.2695 Warp Zone |
ユーザー |
|
提出日時 | 2024-04-16 22:34:23 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 14 ms / 2,000 ms |
コード長 | 1,217 bytes |
コンパイル時間 | 2,523 ms |
コンパイル使用メモリ | 210,192 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-08 04:19:52 |
合計ジャッジ時間 | 3,277 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
#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;}