結果

問題 No.2328 Build Walls
ユーザー keisuke6keisuke6
提出日時 2023-05-28 14:34:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 461 ms / 3,000 ms
コード長 1,532 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 213,068 KB
実行使用メモリ 118,132 KB
最終ジャッジ日時 2023-08-27 10:07:19
合計ジャッジ時間 9,847 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 161 ms
63,812 KB
testcase_14 AC 115 ms
32,804 KB
testcase_15 AC 107 ms
32,540 KB
testcase_16 AC 23 ms
9,636 KB
testcase_17 AC 129 ms
40,992 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 24 ms
11,572 KB
testcase_20 AC 11 ms
6,292 KB
testcase_21 AC 143 ms
58,344 KB
testcase_22 AC 178 ms
46,860 KB
testcase_23 AC 420 ms
117,868 KB
testcase_24 AC 412 ms
118,132 KB
testcase_25 AC 418 ms
117,980 KB
testcase_26 AC 384 ms
117,944 KB
testcase_27 AC 405 ms
117,980 KB
testcase_28 AC 289 ms
117,868 KB
testcase_29 AC 418 ms
117,828 KB
testcase_30 AC 300 ms
117,872 KB
testcase_31 AC 299 ms
117,948 KB
testcase_32 AC 403 ms
118,048 KB
testcase_33 AC 461 ms
117,808 KB
testcase_34 AC 304 ms
117,924 KB
testcase_35 AC 437 ms
117,808 KB
testcase_36 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int H,W;
  cin>>H>>W;
  H -= 2;
  vector<vector<int>> A(H,vector<int>(W));
  for(int i=0;i<H;i++)for(int j=0;j<W;j++){
  	cin>>A[i][j];
  	if(A[i][j] == -1) A[i][j] = 1e18;
  } 
  vector<vector<pair<int,int>>> G(H*W);
  vector<int> dist(H*W,1e18);
  for(int i=0;i<H;i++)for(int j=0;j<W;j++){
  	if(i != 0) G[i*W+j].push_back({(i-1)*W+j,A[i-1][j]});
  	if(j != 0) G[i*W+j].push_back({(i)*W+j-1,A[i][j-1]});
  	if(i != H-1) G[i*W+j].push_back({(i+1)*W+j,A[i+1][j]});
  	if(j != W-1) G[i*W+j].push_back({(i)*W+j+1,A[i][j+1]});
  	if(i != 0 && j != 0) G[i*W+j].push_back({(i-1)*W+j-1,A[i-1][j-1]});
  	if(j != 0 && i != H-1) G[i*W+j].push_back({(i+1)*W+j-1,A[i+1][j-1]});
  	if(i != 0 && j != W-1) G[i*W+j].push_back({(i-1)*W+j+1,A[i-1][j+1]});
  	if(j != W-1 && i != H-1) G[i*W+j].push_back({(i+1)*W+j+1,A[i+1][j+1]});
  }
  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
  for(int i=0;i<H;i++){
  	dist[i*W] = A[i][0];
  	pq.push({dist[i*W],i*W});
  }
  while(!pq.empty()){
  	int u,w;
  	tie(w,u) = pq.top();
  	pq.pop();
  	if(dist[u] != w) continue;
  	for(auto y:G[u]){
  		int v,x;
  		tie(v,x) = y;
  		if(dist[v] <= w+x) continue;
  		dist[v] = w+x;
  		pq.push({dist[v],v});
  	}
  }
  int ans = 1e18;
  for(int i=0;i<H;i++){
  	ans = min(ans,dist[i*W+W-1]);
  }
  /*for(int i=0;i<H;i++){
  	for(int j=0;j<W;j++){
  		cout<<dist[i*W+j]<<' ';
  	}
  	cout<<endl;
  }*/
  cout<<(ans == 1e18 ? -1 : ans)<<endl;
}
0