結果

問題 No.2328 Build Walls
ユーザー dyktr_06dyktr_06
提出日時 2023-04-13 12:05:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 3,000 ms
コード長 1,439 bytes
コンパイル時間 3,681 ms
コンパイル使用メモリ 207,436 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-04-17 16:29:15
合計ジャッジ時間 5,669 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 30 ms
5,888 KB
testcase_14 AC 44 ms
5,376 KB
testcase_15 AC 39 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 39 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 24 ms
5,760 KB
testcase_22 AC 65 ms
5,632 KB
testcase_23 AC 148 ms
8,320 KB
testcase_24 AC 140 ms
8,320 KB
testcase_25 AC 145 ms
8,320 KB
testcase_26 AC 117 ms
8,320 KB
testcase_27 AC 133 ms
8,320 KB
testcase_28 AC 47 ms
8,320 KB
testcase_29 AC 147 ms
8,320 KB
testcase_30 AC 53 ms
8,448 KB
testcase_31 AC 52 ms
8,320 KB
testcase_32 AC 131 ms
8,320 KB
testcase_33 AC 142 ms
8,320 KB
testcase_34 AC 54 ms
8,320 KB
testcase_35 AC 144 ms
8,192 KB
testcase_36 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int h, w; cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    for(int i = 1; i < h - 1; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
        }
    }
    vector<vector<int>> res(h, vector<int>(w, INF));
    using T = tuple<int, int, int>;
    priority_queue<T, vector<T>, greater<T>> q;
    for(int i = 1; i < h - 1; i++){
        if(a[i][0] != -1){
            q.emplace(a[i][0], i, 0);
            res[i][0] = a[i][0];
        }
    }
    while(q.size()){
        auto [cost, x, y] = q.top();
        q.pop();
        if(cost > res[x][y]){
            continue;
        }
        for(int i = 0; i < 8; i++){
            int x2 = x + dx[i], y2 = y + dy[i];
            if(1 <= x2 && x2 < h - 1 && 0 <= y2 && y2 < w){
                if(a[x2][y2] == -1) continue;
                if(res[x2][y2] > cost + a[x2][y2]){
                    res[x2][y2] = cost + a[x2][y2];
                    q.emplace(res[x2][y2], x2, y2);
                }
            }
        }
    }
    int ans = INF;
    for(int i = 1; i < h - 1; i++){
        ans = min(ans, res[i][w - 1]);
    }
    if(ans == INF){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
}
0