結果

問題 No.867 避難経路
ユーザー square1001square1001
提出日時 2019-08-16 22:40:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,267 ms / 6,000 ms
コード長 2,714 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 85,004 KB
実行使用メモリ 119,268 KB
最終ジャッジ日時 2023-10-24 02:14:06
合計ジャッジ時間 38,150 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
116,332 KB
testcase_01 AC 25 ms
116,332 KB
testcase_02 AC 25 ms
116,332 KB
testcase_03 AC 25 ms
116,332 KB
testcase_04 AC 27 ms
116,332 KB
testcase_05 AC 26 ms
116,332 KB
testcase_06 AC 25 ms
116,332 KB
testcase_07 AC 25 ms
116,332 KB
testcase_08 AC 25 ms
116,332 KB
testcase_09 AC 25 ms
116,332 KB
testcase_10 AC 25 ms
116,332 KB
testcase_11 AC 26 ms
116,332 KB
testcase_12 AC 25 ms
116,332 KB
testcase_13 AC 25 ms
116,332 KB
testcase_14 AC 25 ms
116,332 KB
testcase_15 AC 1,220 ms
117,760 KB
testcase_16 AC 1,213 ms
117,756 KB
testcase_17 AC 1,267 ms
117,760 KB
testcase_18 AC 1,169 ms
119,268 KB
testcase_19 AC 1,192 ms
117,760 KB
testcase_20 AC 1,132 ms
117,760 KB
testcase_21 AC 1,157 ms
117,760 KB
testcase_22 AC 1,183 ms
117,760 KB
testcase_23 AC 1,248 ms
117,760 KB
testcase_24 AC 1,163 ms
117,760 KB
testcase_25 AC 1,138 ms
117,708 KB
testcase_26 AC 1,158 ms
117,760 KB
testcase_27 AC 1,174 ms
117,760 KB
testcase_28 AC 1,143 ms
117,744 KB
testcase_29 AC 1,184 ms
117,744 KB
testcase_30 AC 936 ms
117,748 KB
testcase_31 AC 924 ms
117,764 KB
testcase_32 AC 927 ms
117,764 KB
testcase_33 AC 932 ms
117,764 KB
testcase_34 AC 938 ms
117,752 KB
testcase_35 AC 986 ms
117,756 KB
testcase_36 AC 966 ms
117,748 KB
testcase_37 AC 938 ms
117,716 KB
testcase_38 AC 926 ms
117,652 KB
testcase_39 AC 904 ms
117,676 KB
testcase_40 AC 898 ms
119,068 KB
testcase_41 AC 24 ms
112,308 KB
testcase_42 AC 24 ms
112,360 KB
testcase_43 AC 24 ms
112,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0