結果

問題 No.867 避難経路
ユーザー leaf_1415leaf_1415
提出日時 2019-08-22 21:31:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,590 bytes
コンパイル時間 1,337 ms
コンパイル使用メモリ 69,620 KB
実行使用メモリ 132,736 KB
最終ジャッジ日時 2024-04-21 00:23:15
合計ジャッジ時間 37,674 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,344 KB
testcase_01 AC 7 ms
9,088 KB
testcase_02 AC 7 ms
9,088 KB
testcase_03 AC 7 ms
9,216 KB
testcase_04 AC 7 ms
9,216 KB
testcase_05 AC 8 ms
9,216 KB
testcase_06 AC 8 ms
9,216 KB
testcase_07 AC 7 ms
9,344 KB
testcase_08 AC 8 ms
9,216 KB
testcase_09 AC 7 ms
9,088 KB
testcase_10 AC 8 ms
9,216 KB
testcase_11 AC 7 ms
9,216 KB
testcase_12 AC 7 ms
9,216 KB
testcase_13 AC 7 ms
9,216 KB
testcase_14 AC 8 ms
9,344 KB
testcase_15 AC 2,032 ms
132,608 KB
testcase_16 AC 2,048 ms
132,608 KB
testcase_17 AC 2,086 ms
132,608 KB
testcase_18 AC 1,989 ms
132,608 KB
testcase_19 AC 1,965 ms
132,736 KB
testcase_20 AC 1,899 ms
132,736 KB
testcase_21 AC 1,901 ms
132,480 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 7 ms
6,528 KB
testcase_42 AC 6 ms
7,552 KB
testcase_43 AC 6 ms
7,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <queue>
#include <cstdlib>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> S;

llint h, w, Q;
llint sx, sy;
llint a[255][255];
llint dist[255][255][255];
llint dp[255][255];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

void dijkstra(llint k, llint dist[255][255])
{
	for(int y = 1; y <= h; y++){
		for(int x = 1; x <= w; x++){
			dist[x][y] = inf;
		}
	}
	dist[sx][sy] = a[sx][sy]+k*k;
	
	priority_queue< S, vector<S>, greater<S> > Q;
	Q.push( make_pair(a[sx][sy]+k*k, make_pair(sx, sy)) );
	
	llint x, y, d, nx, ny;
	while(Q.size()){
		d = Q.top().first;
		x = Q.top().second.first, y = Q.top().second.second;
		Q.pop();
		if(dist[x][y] < d) continue;
		for(int i = 0; i < 4; i++){
			nx = x + dx[i], ny = y + dy[i];
			if(nx < 1 || nx > w || ny < 1 || ny > h) continue;
			if(dist[nx][ny] > d + a[nx][ny]+k*k){
				dist[nx][ny] = d + a[nx][ny]+k*k;
				Q.push( make_pair(dist[nx][ny], make_pair(nx, ny)) );
			}
		}
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> h >> w;
	cin >> sy >> sx;
	for(int y = 1; y <= h; y++){
		for(int x = 1; x <= w; x++){
			cin >> a[x][y];
		}
	}
	
	for(int i = 0; i < 255; i++) dijkstra(i, dist[i]);
	
	for(int x = 1; x <= w; x++){
		for(int y = 1; y <= h; y++){
			dp[x][y] = inf;
		}
	}
	dp[sx][sy] = a[sx][sy];
	
	for(int x = 0; sx+x <= w; x++){
		for(int y = 0; sy+y <= h; y++){
			int px = sx+x, py = sy+y;
			if(x > 0) dp[px][py] = min(dp[px][py], dp[px-1][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py-1]);
			dp[px][py] += a[px][py];
		}
	}
	for(int x = 0; sx-x >= 1; x++){
		for(int y = 0; sy+y <= h; y++){
			int px = sx-x, py = sy+y;
			if(x > 0) dp[px][py] = min(dp[px][py], dp[px+1][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py-1]);
			dp[px][py] += a[px][py];
		}
	}
	for(int x = 0; sx-x >= 1; x++){
		for(int y = 0; sy-y >= 1; y++){
			int px = sx-x, py = sy-y;
			if(x > 0) dp[px][py] = min(dp[px][py], dp[px+1][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py+1]);
			dp[px][py] += a[px][py];
		}
	}
	for(int x = 0; sx+x <= w; x++){
		for(int y = 0; sy-y >= 1; y++){
			int px = sx+x, py = sy-y;
			if(x > 0) dp[px][py] = min(dp[px][py], dp[px-1][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py+1]);
			dp[px][py] += a[px][py];
		}
	}
	
	cin >> Q;
	llint x, y, k;
	for(int q = 0; q < Q; q++){
		cin >> y >> x >> k;
		if(k <= 250) cout << dist[k][x][y] <<"\n";
		else cout << dp[x][y]+(abs(x-sx)+abs(y-sy)+1)*k*k << "\n";
	}
	flush(cout);
	
	return 0;
}
0