結果

問題 No.2328 Build Walls
ユーザー momoyuu
提出日時 2023-05-28 14:19:30
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 251 ms / 3,000 ms
コード長 1,316 bytes
コンパイル時間 3,086 ms
コンパイル使用メモリ 257,360 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-12-27 00:31:58
合計ジャッジ時間 7,656 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int h,w;
    cin>>h>>w;
    vector<vector<ll> > a(h,vector<ll>(w,0));
    for(int i = 1;i<h-1;i++) for(int j = 0;j<w;j++) cin>>a[i][j];
    vector<vector<ll>>vis(h,vector<ll>(w,1e14));
    using dat = pair<ll,pair<int,int>>;
    priority_queue<dat,vector<dat>,greater<dat>>que;

    for(int i = 1;i<h-1;i++){
        if(a[i][0]==-1) continue;
        que.push(make_pair(a[i][0],make_pair(i,0)));
        vis[i][0] = a[i][0];
    }
    int dx[] = {1,-1,0,0,1,1,-1,-1};
    int dy[] = {0,0,1,-1,1,-1,1,-1};
    while(que.size()){
        dat now = que.top();
        que.pop();
        int ni = now.second.first;
        int nj = now.second.second;
        if(vis[ni][nj]<now.first) continue;
        for(int k = 0;k<8;k++){
            int nni = ni + dx[k];
            int nnj = nj + dy[k];
            if(nni<=0||nni>=h-1||nnj<0||nnj>=w) continue;
            if(vis[nni][nnj]<=vis[ni][nj]+a[nni][nnj]) continue;
            if(a[nni][nnj]==-1) continue;
            vis[nni][nnj] = vis[ni][nj] + a[nni][nnj];
            que.push(make_pair(vis[nni][nnj],make_pair(nni,nnj)));
        }
    }
    ll ans = 1e14;
    for(int i = 1;i<h-1;i++) ans = min(ans,vis[i][w-1]);
    if(ans==1e14) cout<<-1<<endl;
    else cout<<ans<<endl;
}
0