結果

問題 No.34 砂漠の行商人
ユーザー fura
提出日時 2020-06-07 06:10:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,261 ms / 5,000 ms
コード長 935 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 203,604 KB
最終ジャッジ日時 2025-01-10 23:39:49
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:12:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |         scanf("%d%d%d%d%d%d",&n,&v,&sx,&sy,&gx,&gy); sx--; sy--; gx--; gy--;
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:13:32: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |         rep(i,n) rep(j,n) scanf("%d",&B[i][j]);
      |                           ~~~~~^~~~~~~~~~~~~~~

ソースコード

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