結果

問題 No.867 避難経路
ユーザー e869120e869120
提出日時 2019-08-16 14:29:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,200 ms / 6,000 ms
コード長 3,380 bytes
コンパイル時間 1,413 ms
コンパイル使用メモリ 88,328 KB
実行使用メモリ 71,916 KB
最終ジャッジ日時 2024-09-22 06:07:25
合計ジャッジ時間 40,314 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
45,644 KB
testcase_01 AC 13 ms
45,688 KB
testcase_02 AC 14 ms
45,568 KB
testcase_03 AC 12 ms
45,780 KB
testcase_04 AC 12 ms
45,568 KB
testcase_05 AC 13 ms
45,772 KB
testcase_06 AC 12 ms
45,544 KB
testcase_07 AC 13 ms
45,780 KB
testcase_08 AC 13 ms
45,692 KB
testcase_09 AC 13 ms
45,668 KB
testcase_10 AC 12 ms
45,692 KB
testcase_11 AC 12 ms
45,692 KB
testcase_12 AC 12 ms
45,688 KB
testcase_13 AC 13 ms
45,772 KB
testcase_14 AC 13 ms
45,564 KB
testcase_15 AC 1,183 ms
68,632 KB
testcase_16 AC 1,088 ms
69,060 KB
testcase_17 AC 1,200 ms
69,220 KB
testcase_18 AC 1,106 ms
69,220 KB
testcase_19 AC 1,120 ms
70,636 KB
testcase_20 AC 1,065 ms
69,036 KB
testcase_21 AC 1,084 ms
69,224 KB
testcase_22 AC 1,108 ms
69,080 KB
testcase_23 AC 1,160 ms
69,224 KB
testcase_24 AC 1,062 ms
69,220 KB
testcase_25 AC 1,085 ms
69,100 KB
testcase_26 AC 1,087 ms
69,096 KB
testcase_27 AC 1,092 ms
70,764 KB
testcase_28 AC 1,064 ms
70,912 KB
testcase_29 AC 1,120 ms
70,484 KB
testcase_30 AC 904 ms
70,612 KB
testcase_31 AC 929 ms
69,224 KB
testcase_32 AC 921 ms
69,224 KB
testcase_33 AC 913 ms
69,096 KB
testcase_34 AC 916 ms
69,220 KB
testcase_35 AC 982 ms
69,220 KB
testcase_36 AC 976 ms
70,796 KB
testcase_37 AC 928 ms
69,060 KB
testcase_38 AC 925 ms
71,916 KB
testcase_39 AC 904 ms
70,512 KB
testcase_40 AC 897 ms
69,092 KB
testcase_41 AC 14 ms
67,172 KB
testcase_42 AC 14 ms
67,184 KB
testcase_43 AC 15 ms
69,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
#include <cassert>
using namespace std;
#pragma warning (disable: 4996)

const long long BASE = 120;

long long H, W, Q, gx, gy, A[259][259];
long long val[259][259][259], dist[259][259], dist2[259][259], P[259][259];
priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, greater<tuple<long long, int, int>>> Que;

void dijkstra(long long r) {
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) dist[i][j] = (1LL << 62);
	}
	dist[gx][gy] = 1LL * (r*r + A[gx][gy]);
	Que.push(make_tuple(dist[gx][gy], gx, gy));

	int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0,1, 0, -1 };

	while (!Que.empty()) {
		tuple<long long, int, int> tup = Que.top(); Que.pop();
		int cx = get<1>(tup), cy = get<2>(tup);
		for (int i = 0; i < 4; i++) {
			int ex = cx + dx[i], ey = cy + dy[i];
			if (ex <= 0 || ey <= 0 || ex > H || ey > W) continue;

			long long cost = r * r + A[ex][ey];
			if (dist[ex][ey] > dist[cx][cy] + 1LL * cost) {
				dist[ex][ey] = dist[cx][cy] + 1LL * cost;
				Que.push(make_tuple(dist[ex][ey], ex, ey));
			}
		}
	}
}

void init_P() {
	for (int i = 0; i <= H + 1; i++) {
		for (int j = 0; j <= W + 1; j++) P[i][j] = (1LL << 62);
	}
	P[gx][gy] = A[gx][gy];
}

void add_P() {
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) dist2[i][j] = min(dist2[i][j], P[i][j]);
	}
}

void dijkstra2() {
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) dist2[i][j] = (1LL << 62);
	}

	init_P();
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			if (i == gx && j == gy) continue;
			P[i][j] = min(P[i][j], P[i - 1][j] + A[i][j]);
			P[i][j] = min(P[i][j], P[i][j - 1] + A[i][j]);
		}
	}
	add_P();

	init_P();
	for (int i = 1; i <= H; i++) {
		for (int j = W; j >= 1; j--) {
			if (i == gx && j == gy) continue;
			P[i][j] = min(P[i][j], P[i - 1][j] + A[i][j]);
			P[i][j] = min(P[i][j], P[i][j + 1] + A[i][j]);
		}
	}
	add_P();

	init_P();
	for (int i = H; i >= 1; i--) {
		for (int j = 1; j <= W;j++) {
			if (i == gx && j == gy) continue;
			P[i][j] = min(P[i][j], P[i + 1][j] + A[i][j]);
			P[i][j] = min(P[i][j], P[i][j - 1] + A[i][j]);
		}
	}
	add_P();

	init_P();
	for (int i = H; i >= 1; i--) {
		for (int j = W; j >= 1; j--) {
			if (i == gx && j == gy) continue;
			P[i][j] = min(P[i][j], P[i + 1][j] + A[i][j]);
			P[i][j] = min(P[i][j], P[i][j + 1] + A[i][j]);
		}
	}
	add_P();
}

int main() {
	scanf("%lld%lld%lld%lld", &H, &W, &gx, &gy);
	assert(1 <= H && H <= 250);
	assert(1 <= W && W <= 250);
	assert(1 <= gx && gx <= H);
	assert(1 <= gy && gy <= W);
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			scanf("%lld", &A[i][j]);
			assert(1 <= A[i][j] && A[i][j] <= 99);
		}
	}
	for (int i = 1; i <= BASE; i++) {
		dijkstra(i);
		for (int j = 1; j <= H; j++) {
			for (int k = 1; k <= W; k++) val[i][j][k] = dist[j][k];
		}
	}
	dijkstra2();

	scanf("%lld", &Q);
	assert(1 <= Q && Q <= 500000);
	for (int i = 1; i <= Q; i++) {
		long long cx, cy, k;
		scanf("%lld%lld%lld", &cx, &cy, &k);
		assert(1 <= cx && cx <= H);
		assert(1 <= cy && cy <= W);
		assert(1 <= k && k <= 1000000);
		long long cost = 0;
		if (k >= BASE) {
			cost = 1LL * (abs(cx - gx) + abs(cy - gy) + 1LL) * k * k + dist2[cx][cy];
		}
		else {
			cost = val[k][cx][cy];
		}
		printf("%lld\n", cost);
	}
	return 0;
}
0