結果
問題 | No.2695 Warp Zone |
ユーザー |
|
提出日時 | 2024-03-30 17:45:04 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 90 ms / 2,000 ms |
コード長 | 1,319 bytes |
コンパイル時間 | 2,171 ms |
コンパイル使用メモリ | 214,964 KB |
実行使用メモリ | 67,840 KB |
最終ジャッジ日時 | 2024-09-30 17:37:39 |
合計ジャッジ時間 | 3,942 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/segtree>using namespace atcoder;using namespace std;using ll = long long;struct Edge {int to;ll cost;};using Graph = vector<vector<Edge>>;ll INF = (1LL << 60);void DJK(int s, vector<ll>& dist, Graph& G) {dist[s] = 0;priority_queue<pair<ll, int>, vector<pair<ll,int>>, greater<pair<ll, int>>> pq;int N = G.size();for (int v = 0;v < N;v++) pq.emplace(dist[v], v);while (!pq.empty()) {int v = pq.top().second;ll dis = pq.top().first;pq.pop();if (dist[v] < dis) continue;for (auto nv : G[v]) {ll cost = nv.cost + dis;if (cost < dist[nv.to]) {dist[nv.to] = cost;pq.emplace(dist[nv.to], nv.to);}}}}int main() {ll H, W;int N;cin >> H >> W >> N;int K = (2 * N) + 2;vector<pair<ll, ll>> pos;pos.push_back({1, 1});pos.push_back({H, W});Graph G(K);for (int i = 0;i < N;i++) {ll a, b, c,d;cin >> a >> b >> c >> d;pos.push_back({a,b});int x = pos.size() - 1;pos.push_back({c, d});int y = pos.size() - 1;G[x].push_back({y, 1});}for (int i = 0;i < K;i++) for (int j = 0;j < K;j++) {if (i == j) continue;ll dist = abs(pos[i].first - pos[j].first) + abs(pos[i].second - pos[j].second);G[i].push_back({j, dist});}vector<ll> dist(K, INF);DJK(0, dist, G);cout << dist[1] << endl;}