結果

問題 No.867 避難経路
ユーザー risujirohrisujiroh
提出日時 2019-08-16 22:57:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,478 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 191,124 KB
実行使用メモリ 24,108 KB
最終ジャッジ日時 2023-10-24 03:37:56
合計ジャッジ時間 29,926 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 1,628 ms
24,108 KB
testcase_16 AC 1,817 ms
24,108 KB
testcase_17 AC 1,711 ms
24,108 KB
testcase_18 AC 1,604 ms
24,108 KB
testcase_19 AC 1,590 ms
24,108 KB
testcase_20 AC 376 ms
24,108 KB
testcase_21 AC 358 ms
24,108 KB
testcase_22 AC 402 ms
24,108 KB
testcase_23 AC 456 ms
24,108 KB
testcase_24 AC 418 ms
24,108 KB
testcase_25 AC 908 ms
24,108 KB
testcase_26 AC 902 ms
24,108 KB
testcase_27 AC 948 ms
24,108 KB
testcase_28 AC 870 ms
24,108 KB
testcase_29 AC 873 ms
24,108 KB
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct Edge { int to, cost; };

template<class T, class Edge> V<T> dijkstra(const VV<Edge>& g, int s = 0) {
  V<T> dist(g.size(), numeric_limits<T>::max());
  using P = pair<T, int>;
  priority_queue< P, V<P>, greater<P> > pque;
  pque.emplace(dist[s] = 0, s);
  while (!pque.empty()) {
    T d; int v;
    tie(d, v) = pque.top(); pque.pop();
    if (d > dist[v]) continue;
    for (const auto& e : g[v]) if (dist[v] + e.cost < dist[e.to]) {
      pque.emplace(dist[e.to] = dist[v] + e.cost, e.to);
    } 
  }
  return dist;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int h, w; cin >> h >> w;
  int gi, gj; cin >> gi >> gj, --gi, --gj;
  auto in = [&](int i, int j) -> int { return i * w + j; };
  auto out = [&](int i, int j) -> int { return (h + i) * w + j; };
  VV<Edge> g(2 * h * w);
  VV<> a(h, V<>(w));
  for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
    cin >> a[i][j];
    g[in(i, j)].emplace_back(Edge{out(i, j), a[i][j]});
    for (int di = -1; di <= 1; ++di) for (int dj = -1; dj <= 1; ++dj) if (abs(di) + abs(dj) == 1) {
      int ni = i + di, nj = j + dj;
      if (ni < 0 or ni >= h or nj < 0 or nj >= w) continue;
      g[out(i, j)].emplace_back(Edge{in(ni, nj), 0});
    }
  }

  int q; cin >> q;
  struct Q { int t, i, j, k; };
  V<Q> qs(q);
  for (int t = 0; t < q; ++t) {
    qs[t].t = t;
    cin >> qs[t].i >> qs[t].j >> qs[t].k, --qs[t].i, --qs[t].j;
  }
  sort(begin(qs), end(qs), [](const auto& l, const auto& r) { return l.k < r.k; });

  V<lint> res(q);
  auto itr = begin(qs);
  while (itr != end(qs)) {
    lint k = itr->k;
    if (k >= 2500) {
      for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
        g[in(i, j)][0].cost = a[i][j] + 2500 * 2500;
      }
      auto d = dijkstra<lint>(g, in(gi, gj));
      while (itr != end(qs)) {
        int dist = abs(itr->i - gi) + abs(itr->j - gj) + 1;
        res[itr->t] = d[out(itr->i, itr->j)] + dist * ((lint)itr->k * itr->k - 2500 * 2500);
        ++itr;
      }
      break;
    }

    for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
      g[in(i, j)][0].cost = a[i][j] + k * k;
    }
    auto d = dijkstra<lint>(g, in(gi, gj));
    while (itr != end(qs) and itr->k == k) {
      res[itr->t] = d[out(itr->i, itr->j)];
      ++itr;
    }
  }
  for (lint e : res) cout << e << '\n';
}
0