結果
問題 | No.2328 Build Walls |
ユーザー |
|
提出日時 | 2023-07-08 08:36:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 242 ms / 3,000 ms |
コード長 | 1,368 bytes |
コンパイル時間 | 2,100 ms |
コンパイル使用メモリ | 205,856 KB |
最終ジャッジ日時 | 2025-02-15 08:48:31 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
typedef long long ll;typedef long double ld;#include <bits/stdc++.h>using namespace std;#define int long longsigned main(){ll h,w;std::cin >> h>>w;h-=2;vector<vector<ll>> a(h,vector<ll>(w));for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {std::cin >> a[i][j];}}vector<vector<ll>> dis(h,vector<ll>(w,1e18));using P = pair<ll,pair<ll,ll>>;priority_queue<P,vector<P>,greater<P>> pq;for (int i = 0; i < h; i++) {if(a[i][0]==-1)continue;dis[i][0] = a[i][0];pq.push({dis[i][0], {i,0}});}while(!pq.empty()){auto [d, p] = pq.top();pq.pop();auto [i,j] = p;if(dis[i][j]<d)continue;for (int x = -1; x <= 1; x++) {for (int y = -1; y <= 1; y++) {if(i+x<0||i+x>=h||j+y<0||j+y>=w)continue;if(a[i+x][j+y]==-1)continue;if(dis[i+x][j+y]>d+a[i+x][j+y]){dis[i+x][j+y] = d+a[i+x][j+y];pq.push({dis[i+x][j+y], {i+x, j+y}});}}}}ll ans = 1e18;for (int i = 0; i < h; i++) {ans = min(ans, dis[i][w-1]);}if(ans==1e18){std::cout << -1 << std::endl;}else{std::cout << ans << std::endl;}}