結果
問題 | No.2328 Build Walls |
ユーザー | srjywrdnprkt |
提出日時 | 2023-06-04 02:12:09 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 246 ms / 3,000 ms |
コード長 | 1,622 bytes |
コンパイル時間 | 1,573 ms |
コンパイル使用メモリ | 131,936 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-12-29 03:21:48 |
合計ジャッジ時間 | 7,105 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <iostream> #include <vector> #include <cmath> #include <map> #include <set> #include <iomanip> #include <queue> #include <algorithm> #include <numeric> #include <deque> #include <complex> #include <cassert> using namespace std; template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>; int main(){ int H, W, h, w, nh, nw; long long alt, d, ans=1e18; cin >> H >> W; H-=2; pq<tuple<long long, int, int>> que; int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}; int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; vector<vector<long long>> dist(H, vector<long long>(W, 1e18)); vector<vector<bool>> visit(H, vector<bool>(W)); vector<vector<int>> field(H, vector<int>(W)); for (int i=0; i < H; i++){ for (int j=0; j<W; j++) cin >> field[i][j]; } for (int i=0; i<H; i++){ if (field[i][0] == -1) continue; que.push({field[i][0], i, 0}); dist[i][0] = field[i][0]; } while(!que.empty()){ tie(d, h, w) = que.top(); que.pop(); if (visit[h][w]) continue; visit[h][w] = 1; for (int dir=0; dir < 8; dir++){ nh = h + dx[dir]; nw = w + dy[dir]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (field[nh][nw] == -1) continue; alt = d + field[nh][nw]; if (alt < dist[nh][nw]){ dist[nh][nw] = alt; que.push({alt, nh, nw}); } } } for (int i=0; i<H; i++) ans = min(ans, dist[i][W-1]); cout << (ans == 1e18 ? -1 : ans) << endl; return 0; }