結果

問題 No.867 避難経路
ユーザー pekempeypekempey
提出日時 2019-08-16 23:49:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,371 ms / 6,000 ms
コード長 2,079 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 182,444 KB
実行使用メモリ 126,564 KB
最終ジャッジ日時 2023-10-24 10:22:33
合計ジャッジ時間 63,407 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
126,220 KB
testcase_01 AC 48 ms
126,220 KB
testcase_02 AC 48 ms
126,220 KB
testcase_03 AC 47 ms
126,220 KB
testcase_04 AC 48 ms
126,220 KB
testcase_05 AC 48 ms
126,220 KB
testcase_06 AC 47 ms
126,220 KB
testcase_07 AC 48 ms
126,220 KB
testcase_08 AC 48 ms
126,220 KB
testcase_09 AC 47 ms
126,220 KB
testcase_10 AC 48 ms
126,220 KB
testcase_11 AC 47 ms
126,220 KB
testcase_12 AC 48 ms
126,220 KB
testcase_13 AC 48 ms
126,220 KB
testcase_14 AC 48 ms
126,220 KB
testcase_15 AC 2,150 ms
126,500 KB
testcase_16 AC 2,097 ms
126,500 KB
testcase_17 AC 2,203 ms
126,504 KB
testcase_18 AC 2,064 ms
126,500 KB
testcase_19 AC 2,097 ms
126,500 KB
testcase_20 AC 2,055 ms
126,500 KB
testcase_21 AC 2,012 ms
126,500 KB
testcase_22 AC 2,082 ms
126,500 KB
testcase_23 AC 2,176 ms
126,504 KB
testcase_24 AC 2,060 ms
126,500 KB
testcase_25 AC 2,021 ms
126,500 KB
testcase_26 AC 2,104 ms
126,500 KB
testcase_27 AC 2,089 ms
126,500 KB
testcase_28 AC 2,179 ms
126,488 KB
testcase_29 AC 2,371 ms
126,488 KB
testcase_30 AC 1,722 ms
126,488 KB
testcase_31 AC 1,750 ms
126,504 KB
testcase_32 AC 1,717 ms
126,504 KB
testcase_33 AC 1,723 ms
126,504 KB
testcase_34 AC 1,793 ms
126,500 KB
testcase_35 AC 1,920 ms
126,500 KB
testcase_36 AC 1,929 ms
126,496 KB
testcase_37 AC 1,764 ms
126,508 KB
testcase_38 AC 1,757 ms
126,512 KB
testcase_39 AC 1,741 ms
126,520 KB
testcase_40 AC 1,715 ms
126,564 KB
testcase_41 AC 34 ms
126,212 KB
testcase_42 AC 35 ms
126,212 KB
testcase_43 AC 35 ms
126,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

ll dp[250][250][250];
ll ep[250][250];

constexpr ll inf = 1e13;

void chmin(ll &x, ll y) {
  x = min(x, y);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int H, W, GY, GX; cin >> H >> W >> GY >> GX;
  GY--; GX--;
  vector<vector<int>> A(H, vector<int>(W));
  rep(i, H) rep(j, W) cin >> A[i][j];
  rep(k, 250) rep(i, 250) rep(j, 250) dp[k][i][j] = 1e18;
  dp[0][GY][GX] = A[GY][GX];
  const int dy[] = {0, 1, 0, -1};
  const int dx[] = {1, 0, -1, 0};
  rep(k, 250) {
    dp[k][GY][GX] = A[GY][GX] + k*k;
    priority_queue<tuple<ll, int, int>> q;
    q.emplace(-dp[k][GY][GX], GY, GX);
    while (!q.empty()) {
      ll d;
      int y, x;
      tie(d, y, x) = q.top(); q.pop();
      if (-d > dp[k][y][x]) continue;
      rep(l, 4) {
        int ny = y + dy[l];
        int nx = x + dx[l];
        if (0 <= ny && ny < H && 0 <= nx && nx < W) {
          if (dp[k][ny][nx] > dp[k][y][x] + A[ny][nx] + k*k) {
            dp[k][ny][nx] = dp[k][y][x] + A[ny][nx] + k*k;
            q.emplace(-dp[k][ny][nx], ny, nx);
          }
        }
      }
    }
  }
  rep(i, 250) rep(j, 250) ep[i][j] = 1e18;
  ep[GY][GX] = A[GY][GX] + inf;
  priority_queue<tuple<ll, int, int>> q;
  q.emplace(-ep[GY][GX], GY, GX);
  while (!q.empty()) {
    ll d;
    int y, x;
    tie(d, y, x) = q.top(); q.pop();
    if (-d > ep[y][x]) continue;
    rep(l, 4) {
      int ny = y + dy[l];
      int nx = x + dx[l];
      if (0 <= ny && ny < H && 0 <= nx && nx < W) {
        if (ep[ny][nx] > ep[y][x] + A[ny][nx] + inf) {
          ep[ny][nx] = ep[y][x] + A[ny][nx] + inf;
          q.emplace(-ep[ny][nx], ny, nx);
        }
      }
    }
  }
  int Q; cin >> Q;
  while (Q--) {
    int y, x;
    ll k; cin >> y >> x >> k; y--; x--;
    if (k < 250) {
      cout << dp[k][y][x] << '\n';
    } else {
      ll d = (abs(GY - y) + abs(GX - x) + 1);
      cout << ep[y][x] - d * inf + d * k * k << '\n';
    }
  }
}
0