結果

問題 No.867 避難経路
ユーザー lumc_lumc_
提出日時 2019-08-17 06:26:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,126 ms / 6,000 ms
コード長 2,053 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 115,928 KB
実行使用メモリ 113,032 KB
最終ジャッジ日時 2023-10-24 23:44:56
合計ジャッジ時間 35,881 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,532 KB
testcase_01 AC 6 ms
8,532 KB
testcase_02 AC 5 ms
8,532 KB
testcase_03 AC 6 ms
8,532 KB
testcase_04 AC 6 ms
8,532 KB
testcase_05 AC 6 ms
8,532 KB
testcase_06 AC 6 ms
8,532 KB
testcase_07 AC 6 ms
8,532 KB
testcase_08 AC 6 ms
8,532 KB
testcase_09 AC 5 ms
8,532 KB
testcase_10 AC 5 ms
8,532 KB
testcase_11 AC 6 ms
8,532 KB
testcase_12 AC 5 ms
8,532 KB
testcase_13 AC 6 ms
8,532 KB
testcase_14 AC 6 ms
8,532 KB
testcase_15 AC 1,121 ms
112,600 KB
testcase_16 AC 1,068 ms
113,028 KB
testcase_17 AC 1,126 ms
113,032 KB
testcase_18 AC 1,057 ms
113,028 KB
testcase_19 AC 1,053 ms
113,028 KB
testcase_20 AC 1,024 ms
113,028 KB
testcase_21 AC 1,009 ms
113,028 KB
testcase_22 AC 1,023 ms
113,016 KB
testcase_23 AC 1,069 ms
113,020 KB
testcase_24 AC 1,014 ms
113,016 KB
testcase_25 AC 1,004 ms
113,016 KB
testcase_26 AC 1,022 ms
113,016 KB
testcase_27 AC 1,032 ms
113,016 KB
testcase_28 AC 1,072 ms
112,996 KB
testcase_29 AC 1,117 ms
112,996 KB
testcase_30 AC 839 ms
112,996 KB
testcase_31 AC 875 ms
113,020 KB
testcase_32 AC 849 ms
113,020 KB
testcase_33 AC 846 ms
113,020 KB
testcase_34 AC 865 ms
113,016 KB
testcase_35 AC 916 ms
113,016 KB
testcase_36 AC 913 ms
112,952 KB
testcase_37 AC 862 ms
111,756 KB
testcase_38 AC 854 ms
111,756 KB
testcase_39 AC 853 ms
111,788 KB
testcase_40 AC 839 ms
111,796 KB
testcase_41 AC 16 ms
64,120 KB
testcase_42 AC 15 ms
64,700 KB
testcase_43 AC 16 ms
64,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// 右下にゴールがあるとする
// 右下に向かわなかった場合,2 * k^2 の損をする
// 得の最大値は 98 * 248 = 24304 = M
// M < 2 * k^2 <=> k >= 111 = K

// 最短路
// N^2 * log(N^2) * K

int h, w;
int gx, gy;
ll A[250][250];
int q;

constexpr int K = 111;
constexpr ll inf = 1e18;
ll dist[K+1][250][250];
ll steps[K+1][250][250];

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> h >> w;
  cin >> gx >> gy;
  gx--, gy--;
  for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) cin >> A[i][j];
  for(int k = 1; k <= K; k++) {
    for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) dist[k][i][j] = inf;
    using P = tuple<ll, int, int, int>;
    priority_queue<P, vector<P>, greater<P>> pq;
    dist[k][gx][gy] = k * k + A[gx][gy];
    steps[k][gx][gy] = 1;
    pq.emplace(dist[k][gx][gy], gx, gy, 1);
    while(pq.size()) {
      ll d; int x, y, s;
      tie(d, x, y, s) = pq.top();
      pq.pop();
      if(dist[k][x][y] < d) continue;
      for(int dir = 0; dir < 4; dir++) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if(0 <= nx and nx < h and 0 <= ny and ny < w); else continue;
        ll nd = d + k * k + A[nx][ny];
        if(nd < dist[k][nx][ny]) {
          dist[k][nx][ny] = nd;
          steps[k][nx][ny] = s + 1;
          pq.emplace(nd, nx, ny, s + 1);
        }
      }
    }
  }
  cin >> q;
  for(int i = 0; i < q; i++) {
    ll x, y, k;
    cin >> x >> y >> k;
    x--, y--;
    ll r = min<ll>(K, k);
    ll ans = dist[r][x][y] + (k * k - r * r) * steps[r][x][y];
    cout << ans << "\n";
  }
  return 0;
}
0