結果

問題 No.34 砂漠の行商人
ユーザー furafura
提出日時 2020-06-07 06:10:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,393 ms / 5,000 ms
コード長 935 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 210,904 KB
実行使用メモリ 80,964 KB
最終ジャッジ日時 2023-08-25 03:25:17
合計ジャッジ時間 11,568 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
5,544 KB
testcase_02 AC 7 ms
18,124 KB
testcase_03 AC 10 ms
16,152 KB
testcase_04 AC 46 ms
41,364 KB
testcase_05 AC 47 ms
47,584 KB
testcase_06 AC 10 ms
36,780 KB
testcase_07 AC 72 ms
57,868 KB
testcase_08 AC 98 ms
68,248 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 49 ms
43,296 KB
testcase_13 AC 3 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1,782 ms
63,060 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1,049 ms
80,704 KB
testcase_20 AC 2,114 ms
80,964 KB
testcase_21 AC 19 ms
80,440 KB
testcase_22 AC 2,393 ms
78,704 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 165 ms
60,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
const int INF=1<<29;

int main(){
	int n,v,sx,sy,gx,gy,B[100][100];
	scanf("%d%d%d%d%d%d",&n,&v,&sx,&sy,&gx,&gy); sx--; sy--; gx--; gy--;
	rep(i,n) rep(j,n) scanf("%d",&B[i][j]);

	if(v>=2000) return printf("%d\n",abs(sx-gx)+abs(sy-gy)),0;

	static int d[100][100][2000];
	rep(i,n) rep(j,n) rep(k,v+1) d[i][j][k]=INF;
	d[sy][sx][v]=0;
	priority_queue<tuple<int,int,int,int>> Q; Q.emplace(0,sy,sx,v);
	while(!Q.empty()){
		int d0,i,j,k; tie(d0,i,j,k)=Q.top(); Q.pop();
		d0*=-1;
		if(d0>d[i][j][k]) continue;

		rep(l,4){
			int y=i+dy[l],x=j+dx[l];
			if(0<=y && y<n && 0<=x && x<n && B[y][x]<k && d[y][x][k-B[y][x]]>d0+1){
				d[y][x][k-B[y][x]]=d0+1;
				Q.emplace(-(d0+1),y,x,k-B[y][x]);
			}
		}
	}

	int ans=INF;
	rep(k,v+1) ans=min(ans,d[gy][gx][k]);
	printf("%d\n",ans<INF?ans:-1);

	return 0;
}
0