結果
問題 | No.2328 Build Walls |
ユーザー |
![]() |
提出日時 | 2023-05-28 15:19:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,383 bytes |
コンパイル時間 | 2,264 ms |
コンパイル使用メモリ | 198,784 KB |
最終ジャッジ日時 | 2025-02-13 13:00:17 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 WA * 14 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll=long long; using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; const ll INF=(1ll<<60); #define rep(i,n) for (ll i=0;i<(ll)(n);i++) #define all(v) v.begin(),v.end() template<class T> void chmin(T &a,T b){ if(a>b){ a=b; } } template<class T> void chmax(T &a,T b){ if(a<b){ a=b; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll h,w; cin >> h >> w; h-=2; vector<vector<ll>> a(h,vector<ll>(w)); rep(i,h){ rep(j,w){ cin >> a[i][j]; } } vector<vector<ll>> dp(h,vector<ll>(w+1,INF)); rep(i,h) dp[i][0]=0; rep(j,w){ rep(i,h){ if(a[i][j]==-1) continue; ll now=a[i][j]; for(ll k=i-2;0<=k;k--){ if(a[k+1][j]==-1) break; now+=a[k+1][j]; chmin(dp[k][j+1],dp[i][j]+now); } for(ll k=max(0ll,i-1);k<=min(h-1,i+1);k++){ chmin(dp[k][j+1],dp[i][j]+a[i][j]); } now=a[i][j]; for(ll k=i+2;k<h;k++){ if(a[k-1][j]==-1) break; now+=a[k-1][j]; chmin(dp[k][j+1],dp[i][j]+now); } } } ll ans=INF; rep(i,h) chmin(ans,dp[i][w]); if(ans==INF) cout << "-1\n"; else cout << ans << '\n'; }