結果

問題 No.867 避難経路
ユーザー square1001square1001
提出日時 2019-08-16 22:40:40
言語 C++14
(gcc 13.3.0 + boost 1.87.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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

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