結果

問題 No.2328 Build Walls
ユーザー Naoto FujiwaraNaoto Fujiwara
提出日時 2023-05-29 15:07:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 660 ms / 3,000 ms
コード長 2,090 bytes
コンパイル時間 947 ms
コンパイル使用メモリ 97,104 KB
実行使用メモリ 117,280 KB
最終ジャッジ日時 2023-08-27 23:43:00
合計ジャッジ時間 9,588 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 110 ms
25,556 KB
testcase_14 AC 167 ms
30,680 KB
testcase_15 AC 113 ms
21,404 KB
testcase_16 AC 19 ms
6,220 KB
testcase_17 AC 108 ms
20,164 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 13 ms
5,424 KB
testcase_20 AC 11 ms
4,672 KB
testcase_21 AC 64 ms
15,976 KB
testcase_22 AC 270 ms
45,028 KB
testcase_23 AC 453 ms
73,728 KB
testcase_24 AC 411 ms
66,668 KB
testcase_25 AC 443 ms
72,360 KB
testcase_26 AC 328 ms
53,840 KB
testcase_27 AC 389 ms
62,656 KB
testcase_28 AC 122 ms
28,588 KB
testcase_29 AC 450 ms
73,756 KB
testcase_30 AC 198 ms
44,724 KB
testcase_31 AC 178 ms
40,088 KB
testcase_32 AC 383 ms
61,704 KB
testcase_33 AC 660 ms
117,280 KB
testcase_34 AC 205 ms
46,556 KB
testcase_35 AC 526 ms
94,904 KB
testcase_36 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:79:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   79 |         auto [cost,now]=pq.top();
      |              ^

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<tuple>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<ll,int> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;
//const int dx[]={-1,0,1,0};
//const int dy[]={0,-1,0,1};

const int dx[]={-1,0,1,1,1,0,-1,-1};
const int dy[]={-1,-1,-1,0,1,1,1,0};

struct edge{
    int to;
    ll cost;
    edge(int to_,ll cost_):to(to_),cost(cost_){};
};

int main(){

    int H,W;
    cin>>H>>W;
    vector<vector<ll>> A(H-1-2+1,vector<ll>(W,-1));
    for(int i=0;i<H-2;i++){
        for(int j=0;j<W;j++){
            cin>>A[i][j];
        }
    }

    vector<vector<edge>> G(H*W);

    for(int i=0;i<H-2;i++){
        for(int j=0;j<W;j++){
            if(A[i][j]==-1) continue;

            for(int k=0;k<8;k++){
                int toi=dy[k]+i;
                int toj=dx[k]+j;
                if(toi<0 || H-2<=toi || toj<0 || W<=toj) continue;
                if(A[toi][toj]<0) continue;

                int now=i*W+j;
                int to=toi*W+toj;
                G[now].emplace_back(to,A[i][j]);
            }

        }
    }

    int start=(H-2)*W;
    int end=(H-2)*W+1;

    for(int i=0;i<H-2;i++){
        G[start].emplace_back(i*W,0);
        if(A[i][W-1]>=0){
            G[i*W+(W-1)].emplace_back(end,A[i][W-1]);
        }
    }

    //dijkstra  
    vector<ll> dp(H*W,INF);

    priority_queue<P,vector<P>,greater<P>> pq;

    pq.emplace(0,start);
    while(!pq.empty()){

        auto [cost,now]=pq.top();
        pq.pop();

        if(dp[now]<=cost) continue;

        //dp[now]>cost
        dp[now]=cost;

        for(edge e:G[now]){
            
            if(dp[e.to]>(dp[now]+e.cost)){
                pq.emplace(dp[now]+e.cost,e.to);
            }
        }
    }

    cout<<(dp[end]==INF?-1:dp[end])<<endl;

    /*
    for(int i=0;i<H-2;i++){
        for(int j=0;j<W;j++){
            cout<<"dp["<<i<<"]["<<j<<"]="<<dp[i*W+j]<<' ';
        }
        cout<<endl;
    }
    */

}
0