結果

問題 No.2328 Build Walls
ユーザー shobonvipshobonvip
提出日時 2023-05-28 14:19:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 441 ms / 3,000 ms
コード長 1,428 bytes
コンパイル時間 4,566 ms
コンパイル使用メモリ 276,144 KB
実行使用メモリ 73,328 KB
最終ジャッジ日時 2023-08-27 09:31:07
合計ジャッジ時間 12,267 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 145 ms
29,004 KB
testcase_14 AC 115 ms
21,512 KB
testcase_15 AC 107 ms
19,516 KB
testcase_16 AC 21 ms
6,540 KB
testcase_17 AC 122 ms
21,352 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 18 ms
6,404 KB
testcase_20 AC 11 ms
4,964 KB
testcase_21 AC 88 ms
19,068 KB
testcase_22 AC 176 ms
30,012 KB
testcase_23 AC 425 ms
65,348 KB
testcase_24 AC 410 ms
62,844 KB
testcase_25 AC 422 ms
64,924 KB
testcase_26 AC 370 ms
56,536 KB
testcase_27 AC 406 ms
61,136 KB
testcase_28 AC 147 ms
27,896 KB
testcase_29 AC 426 ms
65,408 KB
testcase_30 AC 265 ms
51,552 KB
testcase_31 AC 247 ms
48,252 KB
testcase_32 AC 403 ms
60,644 KB
testcase_33 AC 435 ms
73,328 KB
testcase_34 AC 274 ms
52,536 KB
testcase_35 AC 441 ms
71,720 KB
testcase_36 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef modint998244353 mint;
typedef long long ll;

int main(){
	int h, w; cin >> h >> w;
	vector<vector<int>> a(h-2, vector<int>(w));
	for (int i=0; i<h-2; i++){
		for (int j=0; j<w; j++){
			cin >> a[i][j];
		}
	}
	vector<vector<vector<pair<int,int>>>> ikeru(h-2, vector<vector<pair<int,int>>>(w, vector<pair<int,int>>(0)));
	vector<pair<int,int>> go = {
		pair(0,-1), pair(0,1),
		pair(-1,-1), pair(-1,0), pair(-1,1),
		pair(1,-1), pair(1,0), pair(1,1)
	};
	for (int i=0; i<h-2; i++){
		for (int j=0; j<w; j++){
			for (auto[xx,yy]:go){
				int x = i+xx;
				int y = j+yy;
				if (0<=x && x<h-2 && 0<=y && y<w && a[x][y] != -1){
					ikeru[i][j].push_back(pair(x,y));
				}
			}
		}
	}
	vector<vector<int>> d(h-2, vector<int>(w, 1e9));
	priority_queue<tuple<int,int,int>> pq;
	for (int i=0; i<h-2; i++){
		if (a[i][0] != -1){
			pq.push(make_tuple(-a[i][0], i, 0));
			d[i][0] = a[i][0];
		}
	}
	while(!pq.empty()){
		int t = -get<0>(pq.top());
		int i = get<1>(pq.top());
		int j = get<2>(pq.top());
		pq.pop();
		if (t > d[i][j]) continue;
		for (auto [x, y]: ikeru[i][j]){
			if (t + a[x][y] < d[x][y]){
				d[x][y] = t + a[x][y];
				pq.push(make_tuple(-d[x][y], x, y));
			}
		}
	}
	
	int ans = 1e9;
	for (int i=0; i<h-2; i++){
		ans = min(ans, d[i][w-1]);
	}
	if (ans == 1e9) cout << -1 << endl;
	else cout << ans << endl;
}
0