結果
問題 | No.2328 Build Walls |
ユーザー |
|
提出日時 | 2023-05-28 14:27:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 140 ms / 3,000 ms |
コード長 | 1,361 bytes |
コンパイル時間 | 2,139 ms |
コンパイル使用メモリ | 182,580 KB |
実行使用メモリ | 13,312 KB |
最終ジャッジ日時 | 2024-12-27 01:24:15 |
合計ジャッジ時間 | 5,720 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;int main(){ios::sync_with_stdio(false);cin.tie(0);int h, w;cin >> h >> w;h -= 2;ll INF = 1ll << 60;vector<vector<ll>> A(h, vector<ll>(w)), B(h, vector<ll>(w, 1ll << 60));for(int y = 0; y < h; y++){for(int x = 0; x < w; x++){cin >> A[y][x];if(A[y][x] == -1) A[y][x] = INF;}}priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<tuple<ll, int, int>>> pq;for(int y = 0; y < h; y++){pq.emplace(A[y][0], y, 0);B[y][0] = A[y][0];}while(!pq.empty()){ll d;int y, x;tie(d, y, x) = pq.top();pq.pop();if(d > B[y][x]) continue;for(int dy = -1; dy <= 1; dy++){for(int dx = -1; dx <= 1; dx++){if(dy == 0 && dx == 0) continue;int ny = y + dy, nx = x + dx;if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;ll d2 = d + A[ny][nx];if(d2 >= B[ny][nx]) continue;B[ny][nx] = d2;pq.emplace(d2, ny, nx);}}}ll ans = INF;for(int y = 0; y < h; y++){ans = min(ans, B[y][w - 1]);}if(ans == INF) ans = -1;cout << ans << '\n';}