結果

問題 No.3063 幅優先探索
ユーザー risujirohrisujiroh
提出日時 2020-04-01 22:02:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 1,457 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 179,668 KB
実行使用メモリ 78,588 KB
最終ジャッジ日時 2023-09-09 18:17:17
合計ジャッジ時間 3,029 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 351 ms
78,588 KB
testcase_07 AC 344 ms
78,524 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
}
0