結果

問題 No.2328 Build Walls
ユーザー kotatsugamekotatsugame
提出日時 2023-05-28 14:07:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 271 ms / 3,000 ms
コード長 940 bytes
コンパイル時間 1,217 ms
コンパイル使用メモリ 76,800 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-12-26 23:32:26
合計ジャッジ時間 6,396 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>
using namespace std;
int H,W;
int A[800][800];
long D[800][800];
const int dx[8]={0,1,0,-1,1,1,-1,-1};
const int dy[8]={1,0,-1,0,1,-1,1,-1};
int main()
{
	cin>>H>>W;
	H-=2;
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)
	{
		cin>>A[i][j];
		D[i][j]=9e18;
	}
	priority_queue<pair<long,pair<int,int> > >Q;
	for(int i=0;i<H;i++)if(A[i][0]!=-1)
	{
		D[i][0]=A[i][0];
		Q.push(make_pair(-D[i][0],make_pair(i,0)));
	}
	while(!Q.empty())
	{
		long c=-Q.top().first;
		int x=Q.top().second.first,y=Q.top().second.second;
		Q.pop();
		if(D[x][y]<c)continue;
		for(int r=0;r<8;r++)
		{
			int tx=x+dx[r],ty=y+dy[r];
			if(tx<0||ty<0||tx>=H||ty>=W)continue;
			if(A[tx][ty]==-1)continue;
			long nc=c+A[tx][ty];
			if(D[tx][ty]>nc)
			{
				D[tx][ty]=nc;
				Q.push(make_pair(-nc,make_pair(tx,ty)));
			}
		}
	}
	long ans=9e18;
	for(int i=0;i<H;i++)ans=min(ans,D[i][W-1]);
	if(ans==(long)9e18)ans=-1;
	cout<<ans<<endl;
}
0