結果

問題 No.867 避難経路
ユーザー QCFiumQCFium
提出日時 2019-08-16 22:16:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,065 bytes
コンパイル時間 4,703 ms
コンパイル使用メモリ 184,864 KB
実行使用メモリ 177,624 KB
最終ジャッジ日時 2023-10-24 00:36:25
合計ジャッジ時間 18,010 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

std::pair<int, int> dirs[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main() {
	int h = ri(), w = ri();
	int gx = ri() - 1, gy = ri() - 1;
	int a[h][w];
	for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) a[i][j] = ri();
	
	using T = std::pair<int64_t, std::pair<int, int> >;
	std::vector<std::vector<int64_t> > dist[350];
	for (int i = 1; i <= 350; i++) {
		int64_t cost_base = i * i;
		dist[i].resize(h, std::vector<int64_t>(w, 1000000000000000000));
		std::priority_queue<T, std::vector<T>, std::greater<T> > que;
		que.push({a[gx][gy] + cost_base, {gx, gy}});
		dist[i][gx][gy] = a[gx][gy] + cost_base;
		while (que.size()) {
			auto cur = que.top();
			que.pop();
			for (auto d : dirs) {
				int x = cur.second.first + d.first;
				int y = cur.second.second + d.second;
				if (x < 0 || x >= h || y < 0 || y >= w) continue;
				int64_t new_cost = cur.first + cost_base + a[x][y];
				if (dist[i][x][y] > new_cost) {
					dist[i][x][y] = new_cost;
					que.push({new_cost, {x, y}});
				}
			}
		}
	}
	std::vector<std::vector<int64_t> > normal_dist(h, std::vector<int64_t>(w, 1000000000000000000));
	normal_dist[gx][gy] = a[gx][gy];
	std::queue<std::pair<int, int> > que;
	que.push({gx, gy});
	while (que.size()) {
		auto cur = que.front();
		que.pop();
		for (auto d : dirs) {
			int x = cur.first + d.first;
			int y = cur.second + d.second;
			if (x < 0 || x >= h || y < 0 || y >= w) continue;
			if (std::abs(cur.first - gx) + std::abs(cur.second - gy) >= std::abs(x - gx) + std::abs(y - gy)) continue;
			int64_t new_cost = normal_dist[cur.first][cur.second] + a[x][y];
			if (normal_dist[x][y] > new_cost) {
				normal_dist[x][y] = new_cost;
				que.push({x, y});
			}
		}
	}
	int q = ri();
	for (int i = 0; i < q; i++) {
		int x = ri() - 1;
		int y = ri() - 1;
		int k = ri();
		if (k <= 350) {
			std::cout << dist[k][x][y] << std::endl;
		} else std::cout << normal_dist[x][y] + (int64_t) k * k * (std::abs(x - gx) + std::abs(y - gy) + 1) << std::endl;
	}
	return 0;
}
0