結果

問題 No.2328 Build Walls
ユーザー keisuke6keisuke6
提出日時 2023-05-28 14:25:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,215 bytes
コンパイル時間 4,261 ms
コンパイル使用メモリ 210,684 KB
実行使用メモリ 13,368 KB
最終ジャッジ日時 2023-08-27 09:45:24
合計ジャッジ時間 7,544 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 108 ms
8,756 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 15 ms
4,408 KB
testcase_20 WA -
testcase_21 AC 93 ms
8,252 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 185 ms
13,092 KB
testcase_29 WA -
testcase_30 AC 208 ms
13,092 KB
testcase_31 AC 204 ms
13,096 KB
testcase_32 WA -
testcase_33 AC 198 ms
13,092 KB
testcase_34 AC 203 ms
13,016 KB
testcase_35 AC 210 ms
13,016 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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<int>> dp(H,vector<int>(W,1e18));
  for(int i=0;i<H;i++) dp[i][0] = A[i][0];
  for(int i=0;i<W-1;i++){
  	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
  	for(int j=0;j<H;j++) pq.push({A[j][i],j});
  	while(!pq.empty()){
  		int u,w;
  		tie(w,u) = pq.top();
  		pq.pop();
  		if(dp[u][i] != w) continue;
  		if(u != 0){
  			if(dp[u-1][i] > w+A[u-1][i]){
  				dp[u-1][i] = w+A[u-1][i];
  				pq.push({dp[u-1][i],u-1});
  			}
  		}
  		if(u != H-1){
  			if(dp[u+1][i] > w+A[u+1][i]){
  				dp[u+1][i] = w+A[u+1][i];
  				pq.push({dp[u+1][i],u+1});
  			}
  		}
  	}
  	for(int j=0;j<H;j++){
  		dp[j][i+1] = min(dp[j][i+1],dp[j][i]+A[j][i+1]);
  		if(j != H-1) dp[j+1][i+1] = min(dp[j+1][i+1],dp[j][i]+A[j+1][i+1]);
  		if(j) dp[j-1][i+1] = min(dp[j-1][i+1],dp[j][i]+A[j-1][i+1]);
  	}
  }
  int ans = 1e18;
  for(int i=0;i<H;i++) ans = min(ans,dp[i][W-1]);
  cout<<(ans == 1e18 ? -1 : ans)<<endl;
}
0