結果
問題 | No.867 避難経路 |
ユーザー | square1001 |
提出日時 | 2019-08-16 22:40:40 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,333 ms / 6,000 ms |
コード長 | 2,714 bytes |
コンパイル時間 | 1,017 ms |
コンパイル使用メモリ | 85,300 KB |
実行使用メモリ | 116,224 KB |
最終ジャッジ日時 | 2024-09-22 19:09:22 |
合計ジャッジ時間 | 40,302 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
8,192 KB |
testcase_01 | AC | 7 ms
8,064 KB |
testcase_02 | AC | 7 ms
8,064 KB |
testcase_03 | AC | 7 ms
8,192 KB |
testcase_04 | AC | 7 ms
8,192 KB |
testcase_05 | AC | 6 ms
8,192 KB |
testcase_06 | AC | 7 ms
8,192 KB |
testcase_07 | AC | 7 ms
8,192 KB |
testcase_08 | AC | 7 ms
8,064 KB |
testcase_09 | AC | 7 ms
8,192 KB |
testcase_10 | AC | 7 ms
8,192 KB |
testcase_11 | AC | 6 ms
8,192 KB |
testcase_12 | AC | 7 ms
8,064 KB |
testcase_13 | AC | 7 ms
8,064 KB |
testcase_14 | AC | 7 ms
8,192 KB |
testcase_15 | AC | 1,328 ms
116,104 KB |
testcase_16 | AC | 1,238 ms
116,096 KB |
testcase_17 | AC | 1,333 ms
115,968 KB |
testcase_18 | AC | 1,253 ms
116,096 KB |
testcase_19 | AC | 1,275 ms
116,096 KB |
testcase_20 | AC | 1,219 ms
115,968 KB |
testcase_21 | AC | 1,222 ms
116,040 KB |
testcase_22 | AC | 1,251 ms
115,968 KB |
testcase_23 | AC | 1,304 ms
115,968 KB |
testcase_24 | AC | 1,210 ms
116,224 KB |
testcase_25 | AC | 1,198 ms
115,968 KB |
testcase_26 | AC | 1,243 ms
116,096 KB |
testcase_27 | AC | 1,240 ms
115,968 KB |
testcase_28 | AC | 1,237 ms
115,968 KB |
testcase_29 | AC | 1,254 ms
115,840 KB |
testcase_30 | AC | 1,001 ms
115,968 KB |
testcase_31 | AC | 1,008 ms
116,096 KB |
testcase_32 | AC | 1,023 ms
116,096 KB |
testcase_33 | AC | 1,003 ms
116,096 KB |
testcase_34 | AC | 1,056 ms
115,840 KB |
testcase_35 | AC | 1,093 ms
115,840 KB |
testcase_36 | AC | 1,062 ms
114,944 KB |
testcase_37 | AC | 1,062 ms
112,128 KB |
testcase_38 | AC | 1,022 ms
112,128 KB |
testcase_39 | AC | 1,081 ms
113,336 KB |
testcase_40 | AC | 1,022 ms
113,624 KB |
testcase_41 | AC | 3 ms
5,504 KB |
testcase_42 | AC | 5 ms
6,912 KB |
testcase_43 | AC | 5 ms
6,912 KB |
ソースコード
#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) + 1) * k * k << '\n'; } } return 0; }