結果
問題 | No.2328 Build Walls |
ユーザー | arad |
提出日時 | 2023-05-28 18:56:39 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 249 ms / 3,000 ms |
コード長 | 1,885 bytes |
コンパイル時間 | 4,753 ms |
コンパイル使用メモリ | 268,672 KB |
実行使用メモリ | 13,568 KB |
最終ジャッジ日時 | 2024-12-27 15:29:52 |
合計ジャッジ時間 | 9,221 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h> using namespace std; #include <atcoder/all> using namespace atcoder; using ll = long long; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; using VD = vector<double>; using VVD = vector<VD>; using VS = vector<string>; using P = pair<ll,ll>; using VP = vector<P>; #define rep(i, n) for (ll i = 0; i < ll(n); i++) #define out(x) cout << x << endl #define dout(x) cout << fixed << setprecision(10) << x << endl #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define sz(x) (int)(x.size()) #define re0 return 0 #define pcnt __builtin_popcountll template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr int inf = 1e9; constexpr ll INF = 1e18; //using mint = modint1000000007; using mint = modint998244353; int di[8] = {1,0,-1,0,1,1,-1,-1}; int dj[8] = {0,1,0,-1,1,-1,1,-1}; int main(){ ll h,w; cin >> h >> w; h -= 2; VVL a(h,VL(w)); rep(i,h)rep(j,w) cin >> a[i][j]; VVL dist(h,VL(w,INF)); priority_queue<P,VP,greater<P>> pq; rep(i,h){ if(a[i][0] == -1) continue; dist[i][0] = a[i][0]; pq.emplace(dist[i][0],i*w+0); } while(!pq.empty()){ auto [d,v] = pq.top(); pq.pop(); ll i = v/w; ll j = v%w; if(d > dist[i][j]) continue; rep(v,8){ int ni = i+di[v]; int nj = j+dj[v]; if(ni < 0 || nj < 0 || ni >= h || nj >= w) continue; if(a[ni][nj] == -1) continue; if(chmin(dist[ni][nj],dist[i][j]+a[ni][nj])){ pq.emplace(dist[ni][nj],ni*w+nj); } } } ll ans = INF; rep(i,h) chmin(ans,dist[i][w-1]); if(ans == INF) ans = -1; out(ans); }