結果

問題 No.867 避難経路
ユーザー leaf_1415leaf_1415
提出日時 2019-08-22 21:40:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,062 ms / 6,000 ms
コード長 2,574 bytes
コンパイル時間 595 ms
コンパイル使用メモリ 69,620 KB
実行使用メモリ 132,736 KB
最終ジャッジ日時 2024-04-21 00:36:52
合計ジャッジ時間 54,452 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,216 KB
testcase_01 AC 7 ms
9,216 KB
testcase_02 AC 7 ms
9,216 KB
testcase_03 AC 7 ms
9,216 KB
testcase_04 AC 6 ms
9,216 KB
testcase_05 AC 7 ms
9,216 KB
testcase_06 AC 6 ms
9,216 KB
testcase_07 AC 6 ms
9,088 KB
testcase_08 AC 6 ms
9,216 KB
testcase_09 AC 6 ms
9,216 KB
testcase_10 AC 7 ms
9,216 KB
testcase_11 AC 7 ms
9,088 KB
testcase_12 AC 7 ms
9,216 KB
testcase_13 AC 7 ms
9,216 KB
testcase_14 AC 7 ms
9,216 KB
testcase_15 AC 2,062 ms
132,736 KB
testcase_16 AC 1,871 ms
132,608 KB
testcase_17 AC 2,042 ms
132,608 KB
testcase_18 AC 1,920 ms
132,480 KB
testcase_19 AC 1,926 ms
132,608 KB
testcase_20 AC 1,829 ms
132,608 KB
testcase_21 AC 1,825 ms
132,608 KB
testcase_22 AC 1,807 ms
132,608 KB
testcase_23 AC 1,980 ms
132,608 KB
testcase_24 AC 1,801 ms
132,608 KB
testcase_25 AC 1,887 ms
132,608 KB
testcase_26 AC 1,851 ms
132,608 KB
testcase_27 AC 1,925 ms
132,608 KB
testcase_28 AC 1,879 ms
132,608 KB
testcase_29 AC 1,875 ms
132,608 KB
testcase_30 AC 1,496 ms
132,608 KB
testcase_31 AC 1,585 ms
132,608 KB
testcase_32 AC 1,549 ms
132,608 KB
testcase_33 AC 1,539 ms
132,608 KB
testcase_34 AC 1,577 ms
132,096 KB
testcase_35 AC 1,713 ms
131,840 KB
testcase_36 AC 1,721 ms
131,072 KB
testcase_37 AC 1,608 ms
127,872 KB
testcase_38 AC 1,580 ms
127,744 KB
testcase_39 AC 1,546 ms
127,872 KB
testcase_40 AC 1,563 ms
128,000 KB
testcase_41 AC 4 ms
6,400 KB
testcase_42 AC 4 ms
7,424 KB
testcase_43 AC 5 ms
7,424 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] + a[px][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py-1] + 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] + a[px][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py-1] + 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] + a[px][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py+1] + 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] + a[px][py]);
			if(y > 0) dp[px][py] = min(dp[px][py], dp[px][py+1] + 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