結果

問題 No.2328 Build Walls
ユーザー momoyuumomoyuu
提出日時 2023-05-28 14:19:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 3,000 ms
コード長 1,316 bytes
コンパイル時間 4,974 ms
コンパイル使用メモリ 254,992 KB
実行使用メモリ 13,592 KB
最終ジャッジ日時 2023-08-27 09:30:54
合計ジャッジ時間 7,737 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 67 ms
8,844 KB
testcase_14 AC 67 ms
6,008 KB
testcase_15 AC 58 ms
5,988 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 64 ms
6,728 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 57 ms
8,284 KB
testcase_22 AC 95 ms
7,208 KB
testcase_23 AC 226 ms
13,080 KB
testcase_24 AC 215 ms
13,156 KB
testcase_25 AC 224 ms
13,048 KB
testcase_26 AC 190 ms
13,004 KB
testcase_27 AC 209 ms
13,160 KB
testcase_28 AC 115 ms
12,996 KB
testcase_29 AC 226 ms
13,080 KB
testcase_30 AC 123 ms
13,420 KB
testcase_31 AC 121 ms
13,592 KB
testcase_32 AC 208 ms
13,080 KB
testcase_33 AC 239 ms
13,080 KB
testcase_34 AC 126 ms
13,468 KB
testcase_35 AC 236 ms
13,100 KB
testcase_36 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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