結果

問題 No.2328 Build Walls
ユーザー m_99m_99
提出日時 2023-05-28 14:13:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 218 ms / 3,000 ms
コード長 1,339 bytes
コンパイル時間 4,434 ms
コンパイル使用メモリ 267,696 KB
実行使用メモリ 13,468 KB
最終ジャッジ日時 2023-08-27 09:16:56
合計ジャッジ時間 8,927 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 65 ms
8,428 KB
testcase_14 AC 62 ms
6,148 KB
testcase_15 AC 56 ms
6,112 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 61 ms
6,892 KB
testcase_18 AC 3 ms
4,384 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 56 ms
8,632 KB
testcase_22 AC 92 ms
7,372 KB
testcase_23 AC 216 ms
13,296 KB
testcase_24 AC 207 ms
13,464 KB
testcase_25 AC 215 ms
13,304 KB
testcase_26 AC 184 ms
13,380 KB
testcase_27 AC 201 ms
13,292 KB
testcase_28 AC 113 ms
13,316 KB
testcase_29 AC 216 ms
13,344 KB
testcase_30 AC 122 ms
13,328 KB
testcase_31 AC 119 ms
13,276 KB
testcase_32 AC 201 ms
13,468 KB
testcase_33 AC 217 ms
13,080 KB
testcase_34 AC 128 ms
13,440 KB
testcase_35 AC 218 ms
13,328 KB
testcase_36 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001


int main(){
	
	int H,W;
	cin>>H>>W;
	vector a(H,vector<int>(W,-1));
	rep(i,H){
		if(i==0||i==H-1)continue;
		rep(j,W){
			cin>>a[i][j];
		}
	}
	rep(i,H){
		a[i].insert(a[i].begin(),0);
		a[i].push_back(0);
	}
	vector dis(H,vector<long long>(W+2,Inf64));
	priority_queue<pair<long long,pair<int,int>>,vector<pair<long long,pair<int,int>>>,greater<pair<long long,pair<int,int>>>> Q;
	rep(i,H){
		dis[i][0] = 0;
		Q.emplace(0,make_pair(i,0));
	}
	while(Q.size()>0){
		long long D = Q.top().first;
		int x = Q.top().second.first;
		int y = Q.top().second.second;
		Q.pop();
		if(dis[x][y]!=D)continue;
		for(int i=-1;i<=1;i++){
			for(int j=-1;j<=1;j++){
				int xx = x+i,yy = y+j;
				if(xx<0||xx>=H||yy<0||yy>=W+2)continue;
				//cout<<xx<<','<<yy<<endl;
				if(a[xx][yy] == -1)continue;
				long long nD = D + a[xx][yy];
				if(dis[xx][yy]>nD){
					dis[xx][yy] = nD;
					Q.emplace(nD,make_pair(xx,yy));
				}
			}
		}
	}
	/*rep(i,H){
		rep(j,W+2){
		cout<<dis[i][j]<<',';
		}
		cout<<endl;
	}
	*/
	long long ans = dis[0].back();
	if(ans==Inf64)ans = -1;
	cout<<ans<<endl;
	
	return 0;
}
0