結果

問題 No.2328 Build Walls
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-04 02:12:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 3,000 ms
コード長 1,622 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 129,912 KB
実行使用メモリ 11,352 KB
最終ジャッジ日時 2023-08-28 08:07:22
合計ジャッジ時間 6,830 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 65 ms
7,400 KB
testcase_14 AC 65 ms
5,564 KB
testcase_15 AC 57 ms
5,364 KB
testcase_16 AC 12 ms
4,380 KB
testcase_17 AC 62 ms
5,900 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 9 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 54 ms
7,072 KB
testcase_22 AC 98 ms
6,384 KB
testcase_23 AC 224 ms
11,128 KB
testcase_24 AC 215 ms
11,020 KB
testcase_25 AC 226 ms
11,188 KB
testcase_26 AC 191 ms
11,016 KB
testcase_27 AC 210 ms
11,352 KB
testcase_28 AC 110 ms
10,996 KB
testcase_29 AC 225 ms
11,056 KB
testcase_30 AC 118 ms
10,996 KB
testcase_31 AC 117 ms
11,116 KB
testcase_32 AC 208 ms
11,148 KB
testcase_33 AC 228 ms
11,056 KB
testcase_34 AC 118 ms
11,044 KB
testcase_35 AC 225 ms
11,096 KB
testcase_36 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert> 

using namespace std;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
 
int main(){
 
    int H, W, h, w, nh, nw;
    long long alt, d, ans=1e18;
    cin >> H >> W;
    H-=2;
    
    pq<tuple<long long, int, int>> que;
    int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
    int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    vector<vector<long long>> dist(H, vector<long long>(W, 1e18));
    vector<vector<bool>> visit(H, vector<bool>(W));
    vector<vector<int>> field(H, vector<int>(W));
 
    for (int i=0; i < H; i++){
        for (int j=0; j<W; j++) cin >> field[i][j];
    }

    for (int i=0; i<H; i++){
        if (field[i][0] == -1) continue;
        que.push({field[i][0], i, 0});
        dist[i][0] = field[i][0];
    }
 
    while(!que.empty()){
        tie(d, h, w) = que.top();
        que.pop();
        if (visit[h][w]) continue;
        visit[h][w] = 1;
        for (int dir=0; dir < 8; dir++){
            nh = h + dx[dir];
            nw = w + dy[dir];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == -1) continue;
            alt = d + field[nh][nw];
            if (alt < dist[nh][nw]){
                dist[nh][nw] = alt;
                que.push({alt, nh, nw});
            }
        }
    }

    for (int i=0; i<H; i++) ans = min(ans, dist[i][W-1]);

    cout << (ans == 1e18 ? -1 : ans) << endl;

    return 0;
}
0