結果
| 問題 |
No.8063 幅優先探索
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2020-04-01 22:02:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 361 ms / 2,000 ms |
| コード長 | 1,457 bytes |
| コンパイル時間 | 1,823 ms |
| コンパイル使用メモリ | 180,752 KB |
| 実行使用メモリ | 78,580 KB |
| 最終ジャッジ日時 | 2024-06-27 10:53:30 |
| 合計ジャッジ時間 | 3,029 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; };
template <class T> struct dijkstra {
struct edge {
int to;
T w;
};
const T inf = numeric_limits<T>::max();
vector<vector<edge>> g;
dijkstra(int n) : g(n) {}
void add(int from, int to, T w, bool directed = true) {
g[from].push_back({to, w});
if (not directed) g[to].push_back({from, w});
}
vector<T> run(int s) const {
vector<T> d(g.size(), inf);
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<>> pq;
pq.emplace(d[s] = 0, s);
while (not pq.empty()) {
T dv;
int v;
tie(dv, v) = pq.top(), pq.pop();
if (dv != d[v]) continue;
for (auto e : g[v])
if (chmin(d[e.to], dv + e.w)) pq.emplace(d[e.to], e.to);
}
return d;
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
int sx, sy;
cin >> sx >> sy;
--sx, --sy;
int gx, gy;
cin >> gx >> gy;
--gx, --gy;
vector<string> s(h);
for (auto&& e : s) {
cin >> e;
}
auto $ = [&](int i, int j) {
return i * w + j;
};
dijkstra<int> g(h * w);
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (i + 1 < h) {
g.add($(i, j), $(i + 1, j), 1, false);
}
if (j + 1 < w) {
g.add($(i, j), $(i, j + 1), 1, false);
}
}
}
cout << g.run($(sx, sy))[$(gx, gy)] << '\n';
}
risujiroh