結果

問題 No.867 避難経路
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-11 14:22:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,335 bytes
コンパイル時間 1,760 ms
コンパイル使用メモリ 147,128 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-03-11 14:22:25
合計ジャッジ時間 20,196 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 365 ms
8,320 KB
testcase_21 AC 331 ms
8,320 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 301 ms
8,320 KB
testcase_29 AC 289 ms
8,320 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 312 ms
8,320 KB
testcase_35 AC 277 ms
8,320 KB
testcase_36 AC 276 ms
8,192 KB
testcase_37 AC 277 ms
7,936 KB
testcase_38 AC 276 ms
7,936 KB
testcase_39 AC 284 ms
7,936 KB
testcase_40 AC 285 ms
7,936 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
void Main()
{
	int H,W,gi,gj;
	cin >> H >> W >> gi >> gj;
	gi--; gj--;
	vector<vector<int>> A(H,vector<int>(W));
	for(int i = 0;i < H;i++)
	{
		for(int j = 0;j < W;j++)
		{
			cin >> A[i][j];
		}
	}
	vector<vector<int>> cost(H,vector<int>(W,(int)1e9));
	const int d[5] = {0,1,0,-1};
	cost[gi][gj] = A[gi][gj];
	{
		queue<pair<int,int>> Q;
		Q.push(make_pair(gi,gj));
		while(!Q.empty())
		{
			auto [i,j] = Q.front();
			Q.pop();
			for(int k = 0;k < 4;k++)
			{
				int ni = i + d[k];
				int nj = j + d[k + 1];
				if(ni < 0 || nj < 0 || ni >= H || nj >= W)
				{
					continue;
				}
				if(cost[ni][nj] == (int)1e9)
				{
					Q.push(make_pair(ni,nj));
				}
				cost[ni][nj] = min(cost[ni][nj],cost[i][j] + A[ni][nj]);
			}
		}
	}
	vector<vector<vector<int>>> dp(H,vector<vector<int>>(W,vector<int>(10,(int)1e9)));
	for(int k = 1;k <= 9;k++)
	{
		dp[gi][gj][k] = k * k + A[gi][gj];
		priority_queue<pair<int,pair<int,int>>> Q;
		Q.push(make_pair(-dp[gi][gj][k],make_pair(gi,gj)));
		while(!Q.empty())
		{
			int cur = - Q.top().first;
			auto [i,j] = Q.top().second;
			Q.pop();
			if(dp[i][j][k] < cur)
			{
				continue;
			}
			for(int r = 0;r < 4;r++)
			{
				int ni = i + d[r];
				int nj = j + d[r + 1];
				if(ni < 0 || nj < 0 || ni >= H || nj >= W)
				{
					continue;
				}
				if(dp[ni][nj][k] > dp[i][j][k] + k * k + A[ni][nj])
				{
					dp[ni][nj][k] = dp[i][j][k] + k * k + A[ni][nj];
					Q.push(make_pair(-dp[ni][nj][k],make_pair(ni,nj)));
				}
			}
		}
	}
	int Q;
	cin >> Q;
	for(;Q--;)
	{
		int x,y,k;
		cin >> x >> y >> k;
		x--; y--;
		long long ans = 0;
		if(k >= 10)
		{
			ans = (long long) k * k * (abs(x - gi) + abs(y - gj) + 1) + cost[x][y];
		}
		else
		{
			ans = dp[x][y][k];
		}
		cout << ans << "\n";
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}

0