結果
問題 |
No.2328 Build Walls
|
ユーザー |
|
提出日時 | 2023-05-28 15:33:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,071 bytes |
コンパイル時間 | 1,988 ms |
コンパイル使用メモリ | 198,388 KB |
最終ジャッジ日時 | 2025-02-13 13:32:45 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 WA * 15 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll INF64 = 1LL<<60; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main() { int h, w; cin >> h >> w; h -= 2; vector a(h, vector(w, 0ll)); 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] = INF64; } } } vector dp(h, vector(w, INF64)); for (int i = 0; i < h; i++) { dp[i][0] = a[i][0]; } for (int j = 0; j < w - 1; j++) { for (int i = 0; i < h; i++) { for (const auto diff : { -1, 0, 1 }) { auto nxt = i + diff; if (0 <= nxt && nxt < h) { chmin(dp[nxt][j + 1], a[nxt][j + 1] + dp[i][j]); } } } } ll ans = INF64; for (int i = 0; i < h; i++) { chmin(ans, dp[i][w - 1]); } cout << (ans == INF64 ? -1 : ans) << endl; return 0; }