結果

問題 No.2328 Build Walls
ユーザー MM
提出日時 2023-05-28 15:36:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,090 bytes
コンパイル時間 1,784 ms
コンパイル使用メモリ 172,436 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-12-27 07:37:22
合計ジャッジ時間 5,706 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
using namespace std;
using ll = long long;
int main(){
  // input
  int H,W; cin >> H >> W;
  H -= 2;
  
  vector<vector<ll>> A(W,vector<ll>(H));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> A[j][i];
  
  // solve: dp
  vector<vector<ll>> dp(W+1,vector<ll>(H,9e18));
  for (int j = 0; j < H; j++)
    dp[0][j] = 0;
  
  for (int i = 0; i < W; i++){
    for (int j = 0; j < H; j++){
      ll cost = A[i][j];
      if(cost == -1) continue;
      
      chmin(dp[i+1][j],dp[i][j]+cost);
      
      ll cos_u = cost;
      for (int k = j - 1; k >= 0; k--){
        chmin(dp[i+1][j],dp[i][k]+cos_u);
        if(A[i][k] > 0) cos_u += A[i][k];
        else break;
      }
      
      ll cos_d = cost;
      for (int k = j + 1; k < H; k++){
        chmin(dp[i+1][j],dp[i][k]+cos_d);
        if(A[i][k] > 0) cos_d += A[i][k];
        else break;
      }
    }
  }
  
  // output
  ll ans = 9e18;
  for (int j = 0; j < H; j++)    
    ans = min(ans,dp[W][j]);
  
  cout << (ans < 9e18? ans : -1) << endl;
}
0