結果

問題 No.2328 Build Walls
ユーザー kotatsugamekotatsugame
提出日時 2023-05-28 14:07:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 940 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 76,772 KB
実行使用メモリ 11,148 KB
最終ジャッジ日時 2023-08-27 09:05:11
合計ジャッジ時間 5,059 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,468 KB
testcase_01 AC 2 ms
5,508 KB
testcase_02 AC 2 ms
5,420 KB
testcase_03 AC 2 ms
5,572 KB
testcase_04 AC 2 ms
5,428 KB
testcase_05 AC 2 ms
5,412 KB
testcase_06 AC 2 ms
5,424 KB
testcase_07 AC 2 ms
5,460 KB
testcase_08 AC 2 ms
5,424 KB
testcase_09 AC 2 ms
5,464 KB
testcase_10 AC 2 ms
5,396 KB
testcase_11 AC 2 ms
5,460 KB
testcase_12 AC 2 ms
5,452 KB
testcase_13 AC 64 ms
10,264 KB
testcase_14 AC 61 ms
9,952 KB
testcase_15 AC 55 ms
10,104 KB
testcase_16 AC 11 ms
5,824 KB
testcase_17 AC 60 ms
9,708 KB
testcase_18 AC 3 ms
5,872 KB
testcase_19 AC 11 ms
6,460 KB
testcase_20 AC 7 ms
5,856 KB
testcase_21 AC 55 ms
10,448 KB
testcase_22 AC 89 ms
10,728 KB
testcase_23 AC 203 ms
10,980 KB
testcase_24 AC 195 ms
11,148 KB
testcase_25 AC 201 ms
10,872 KB
testcase_26 AC 174 ms
10,868 KB
testcase_27 AC 191 ms
10,976 KB
testcase_28 AC 109 ms
10,860 KB
testcase_29 AC 202 ms
10,932 KB
testcase_30 AC 116 ms
10,864 KB
testcase_31 AC 115 ms
10,860 KB
testcase_32 AC 190 ms
10,980 KB
testcase_33 AC 210 ms
10,876 KB
testcase_34 AC 119 ms
10,908 KB
testcase_35 AC 205 ms
10,876 KB
testcase_36 AC 2 ms
5,412 KB
権限があれば一括ダウンロードができます

ソースコード

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