結果

問題 No.867 避難経路
ユーザー square1001square1001
提出日時 2019-08-16 22:37:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,710 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 85,260 KB
実行使用メモリ 119,292 KB
最終ジャッジ日時 2023-10-24 01:55:45
合計ジャッジ時間 36,392 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
116,356 KB
testcase_01 AC 23 ms
116,356 KB
testcase_02 AC 24 ms
116,356 KB
testcase_03 AC 24 ms
116,356 KB
testcase_04 AC 24 ms
116,356 KB
testcase_05 AC 24 ms
116,356 KB
testcase_06 AC 24 ms
116,356 KB
testcase_07 AC 24 ms
116,356 KB
testcase_08 AC 25 ms
116,356 KB
testcase_09 AC 23 ms
116,356 KB
testcase_10 AC 24 ms
116,356 KB
testcase_11 AC 23 ms
116,356 KB
testcase_12 AC 23 ms
116,356 KB
testcase_13 AC 23 ms
116,356 KB
testcase_14 AC 24 ms
116,356 KB
testcase_15 AC 1,220 ms
119,292 KB
testcase_16 AC 1,160 ms
119,288 KB
testcase_17 AC 1,251 ms
119,292 KB
testcase_18 AC 1,160 ms
119,292 KB
testcase_19 AC 1,193 ms
119,292 KB
testcase_20 AC 1,128 ms
119,292 KB
testcase_21 AC 1,149 ms
119,292 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 23 ms
116,320 KB
testcase_42 AC 23 ms
116,336 KB
testcase_43 AC 23 ms
116,336 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)) * k * k << '\n';
		}
	}
	return 0;
}
0