結果

問題 No.2328 Build Walls
ユーザー erbowlerbowl
提出日時 2023-07-08 08:36:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 3,000 ms
コード長 1,368 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 211,088 KB
実行使用メモリ 13,320 KB
最終ジャッジ日時 2023-09-29 09:52:27
合計ジャッジ時間 8,372 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 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,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 66 ms
8,696 KB
testcase_14 AC 64 ms
5,996 KB
testcase_15 AC 56 ms
5,952 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 61 ms
6,704 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 10 ms
4,380 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 56 ms
8,280 KB
testcase_22 AC 94 ms
7,312 KB
testcase_23 AC 218 ms
13,160 KB
testcase_24 AC 211 ms
13,024 KB
testcase_25 AC 218 ms
13,148 KB
testcase_26 AC 186 ms
13,020 KB
testcase_27 AC 204 ms
13,028 KB
testcase_28 AC 115 ms
13,112 KB
testcase_29 AC 219 ms
13,132 KB
testcase_30 AC 122 ms
13,060 KB
testcase_31 AC 121 ms
13,172 KB
testcase_32 AC 203 ms
13,020 KB
testcase_33 AC 221 ms
13,320 KB
testcase_34 AC 125 ms
13,120 KB
testcase_35 AC 222 ms
13,156 KB
testcase_36 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    ll h,w;
    std::cin >> h>>w;
    h-=2;
    vector<vector<ll>> a(h,vector<ll>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            std::cin >> a[i][j];
        }
    }
    vector<vector<ll>> dis(h,vector<ll>(w,1e18));
    using P = pair<ll,pair<ll,ll>>;
    priority_queue<P,vector<P>,greater<P>> pq;
    for (int i = 0; i < h; i++) {
        if(a[i][0]==-1)continue;
        dis[i][0] = a[i][0];
        pq.push({dis[i][0], {i,0}});
    }

    while(!pq.empty()){
        auto [d, p] = pq.top();pq.pop();
        auto [i,j] = p;
        
        if(dis[i][j]<d)continue;
        
        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                if(i+x<0||i+x>=h||j+y<0||j+y>=w)continue;
                if(a[i+x][j+y]==-1)continue;
                 if(dis[i+x][j+y]>d+a[i+x][j+y]){
                     dis[i+x][j+y] = d+a[i+x][j+y];
                     pq.push({dis[i+x][j+y], {i+x, j+y}});
                 }
            }
        }
    }
    
    ll ans = 1e18;
    
    for (int i = 0; i < h; i++) {
        ans = min(ans, dis[i][w-1]);
    }
    if(ans==1e18){
        std::cout << -1 << std::endl;
    }else{
        std::cout << ans << std::endl;
    }
}
0