結果

問題 No.867 避難経路
ユーザー QCFiumQCFium
提出日時 2019-08-16 22:13:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,222 bytes
コンパイル時間 4,131 ms
コンパイル使用メモリ 186,340 KB
実行使用メモリ 128,996 KB
最終ジャッジ日時 2023-10-24 00:22:58
合計ジャッジ時間 91,973 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 4 ms
4,348 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 2,973 ms
128,996 KB
testcase_16 AC 2,864 ms
128,996 KB
testcase_17 AC 3,038 ms
128,996 KB
testcase_18 AC 2,837 ms
128,996 KB
testcase_19 AC 2,805 ms
128,996 KB
testcase_20 AC 2,741 ms
128,996 KB
testcase_21 AC 2,724 ms
128,996 KB
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 AC 22 ms
4,348 KB
testcase_42 AC 3 ms
4,348 KB
testcase_43 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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 <= 250; 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}});
				}
			}
		}
	}
	int64_t normal_dist[h][w];
	for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) normal_dist[i][j] = 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});
			}
		}
	}
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) std::cerr << normal_dist[i][j] << " ";
		std::cerr << std::endl;
	}
	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