結果

問題 No.2328 Build Walls
ユーザー t98slidert98slider
提出日時 2023-05-28 14:27:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 3,000 ms
コード長 1,361 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 180,132 KB
実行使用メモリ 13,436 KB
最終ジャッジ日時 2023-08-27 09:51:44
合計ジャッジ時間 5,677 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 31 ms
8,332 KB
testcase_14 AC 43 ms
5,768 KB
testcase_15 AC 34 ms
5,824 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 35 ms
6,488 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 25 ms
7,992 KB
testcase_22 AC 65 ms
7,108 KB
testcase_23 AC 133 ms
13,284 KB
testcase_24 AC 125 ms
13,436 KB
testcase_25 AC 130 ms
13,292 KB
testcase_26 AC 106 ms
13,412 KB
testcase_27 AC 120 ms
13,288 KB
testcase_28 AC 50 ms
13,320 KB
testcase_29 AC 131 ms
13,328 KB
testcase_30 AC 56 ms
13,084 KB
testcase_31 AC 54 ms
13,012 KB
testcase_32 AC 119 ms
13,288 KB
testcase_33 AC 149 ms
13,296 KB
testcase_34 AC 57 ms
13,316 KB
testcase_35 AC 142 ms
13,320 KB
testcase_36 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    h -= 2;
    ll INF = 1ll << 60;
    vector<vector<ll>> A(h, vector<ll>(w)), B(h, vector<ll>(w, 1ll << 60));
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            cin >> A[y][x];
            if(A[y][x] == -1) A[y][x] = INF;
        }
    }
    priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<tuple<ll, int, int>>> pq;
    for(int y = 0; y < h; y++){
        pq.emplace(A[y][0], y, 0);
        B[y][0] = A[y][0];
    }
    while(!pq.empty()){
        ll d;
        int y, x;
        tie(d, y, x) = pq.top();
        pq.pop();
        if(d > B[y][x]) continue;
        for(int dy = -1; dy <= 1; dy++){
            for(int dx = -1; dx <= 1; dx++){
                if(dy == 0 && dx == 0) continue;
                int ny = y + dy, nx = x + dx;
                if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
                ll d2 = d + A[ny][nx];
                if(d2 >= B[ny][nx]) continue;
                B[ny][nx] = d2;
                pq.emplace(d2, ny, nx);
            }
        }
    }
    ll ans = INF;
    for(int y = 0; y < h; y++){
        ans = min(ans, B[y][w - 1]);
    }
    if(ans == INF) ans = -1;
    cout << ans << '\n';
}
0