結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
5,596 KB
testcase_02 AC 6 ms
18,088 KB
testcase_03 AC 4 ms
15,984 KB
testcase_04 AC 46 ms
41,128 KB
testcase_05 AC 48 ms
47,400 KB
testcase_06 AC 10 ms
36,980 KB
testcase_07 AC 74 ms
57,900 KB
testcase_08 AC 100 ms
68,220 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 34 ms
43,196 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 20 ms
62,532 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 906 ms
80,440 KB
testcase_20 AC 1,683 ms
80,788 KB
testcase_21 AC 20 ms
80,380 KB
testcase_22 AC 42 ms
77,260 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 111 ms
60,356 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;

		if(i==gy && j==gx) return printf("%d\n",d0),0;
		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]);
			}
		}
	}

	puts("-1");

	return 0;
}
0