結果
問題 | No.867 避難経路 |
ユーザー |
![]() |
提出日時 | 2019-08-16 22:37:43 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,710 bytes |
コンパイル時間 | 1,080 ms |
コンパイル使用メモリ | 85,424 KB |
実行使用メモリ | 118,344 KB |
最終ジャッジ日時 | 2024-09-22 18:49:54 |
合計ジャッジ時間 | 36,562 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 22 WA * 19 |
ソースコード
#include <cmath>#include <queue>#include <iostream>#include <algorithm>using namespace std;const long long inf = 1LL << 61;const int limit = 223; // more than sqrt((H + W) * 99)const int dx[] = { 0, 1, 0, -1 };const int dy[] = { 1, 0, -1, 0 };struct state {int x, y; long long cost;bool operator<(const state& s) const {return cost > s.cost;}};int H, W, Q, gx, gy, A[255][255]; long long distA[255][255], distB[limit + 10][255][255];int main() {cin.tie(0);ios_base::sync_with_stdio(false);cin >> H >> W >> gx >> gy; --gx, --gy;for (int i = 0; i < H; ++i) {for (int j = 0; j < W; ++j) {cin >> A[i][j];distA[i][j] = inf;}}distA[gx][gy] = A[gx][gy];for (int i = gx; i >= 0; --i) {for (int j = 0; j < W; ++j) {if (i < gx) distA[i][j] = min(distA[i][j], distA[i + 1][j] + A[i][j]);if (j < gy) distA[i][j] = min(distA[i][j], distA[i][j + 1] + A[i][j]);if (j > gy) distA[i][j] = min(distA[i][j], distA[i][j - 1] + A[i][j]);}for (int j = W - 1; j >= 0; --j) {if (i < gx) distA[i][j] = min(distA[i][j], distA[i + 1][j] + A[i][j]);if (j < gy) distA[i][j] = min(distA[i][j], distA[i][j + 1] + A[i][j]);if (j > gy) distA[i][j] = min(distA[i][j], distA[i][j - 1] + A[i][j]);}}for (int i = gx + 1; i < H; ++i) {for (int j = 0; j < W; ++j) {distA[i][j] = min(distA[i][j], distA[i - 1][j] + A[i][j]);if (j < gy) distA[i][j] = min(distA[i][j], distA[i][j + 1] + A[i][j]);if (j > gy) distA[i][j] = min(distA[i][j], distA[i][j - 1] + A[i][j]);}for (int j = W - 1; j >= 0; --j) {distA[i][j] = min(distA[i][j], distA[i - 1][j] + A[i][j]);if (j < gy) distA[i][j] = min(distA[i][j], distA[i][j + 1] + A[i][j]);if (j > gy) distA[i][j] = min(distA[i][j], distA[i][j - 1] + A[i][j]);}}for (int i = 1; i <= limit; ++i) {for (int j = 0; j < H; ++j) {for (int k = 0; k < W; ++k) {distB[i][j][k] = inf;}}distB[i][gx][gy] = i * i + A[gx][gy];priority_queue<state> que;que.push(state{ gx, gy, distB[i][gx][gy] });while (!que.empty()) {state u = que.top(); que.pop();if (distB[i][u.x][u.y] < u.cost) continue;for (int j = 0; j < 4; ++j) {int tx = u.x + dx[j], ty = u.y + dy[j];if (0 <= tx && tx < H && 0 <= ty && ty < W) {long long d = distB[i][u.x][u.y] + i * i + A[tx][ty];if (distB[i][tx][ty] > d) {distB[i][tx][ty] = d;que.push(state{ tx, ty, d });}}}}}cin >> Q;for (int i = 0; i < Q; ++i) {int x, y, k;cin >> x >> y >> k; --x, --y;if (k <= limit) {cout << distB[k][x][y] << '\n';}else {cout << distA[x][y] + (long long)(abs(x - gx) + abs(y - gy)) * k * k << '\n';}}return 0;}