結果

問題 No.2328 Build Walls
ユーザー pockynypockyny
提出日時 2023-05-28 14:47:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 701 ms / 3,000 ms
コード長 1,911 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 81,888 KB
実行使用メモリ 197,088 KB
最終ジャッジ日時 2023-08-27 10:36:14
合計ジャッジ時間 9,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
99,588 KB
testcase_01 AC 33 ms
99,596 KB
testcase_02 AC 32 ms
99,596 KB
testcase_03 AC 32 ms
99,656 KB
testcase_04 AC 33 ms
99,700 KB
testcase_05 AC 32 ms
99,752 KB
testcase_06 AC 33 ms
99,760 KB
testcase_07 AC 33 ms
99,720 KB
testcase_08 AC 32 ms
99,600 KB
testcase_09 AC 33 ms
99,588 KB
testcase_10 AC 33 ms
99,580 KB
testcase_11 AC 33 ms
99,576 KB
testcase_12 AC 32 ms
99,648 KB
testcase_13 AC 127 ms
114,900 KB
testcase_14 AC 202 ms
124,512 KB
testcase_15 AC 144 ms
115,712 KB
testcase_16 AC 49 ms
103,604 KB
testcase_17 AC 132 ms
112,112 KB
testcase_18 AC 35 ms
100,276 KB
testcase_19 AC 44 ms
102,960 KB
testcase_20 AC 43 ms
103,016 KB
testcase_21 AC 95 ms
106,520 KB
testcase_22 AC 305 ms
135,996 KB
testcase_23 AC 457 ms
153,196 KB
testcase_24 AC 422 ms
146,424 KB
testcase_25 AC 453 ms
151,968 KB
testcase_26 AC 336 ms
133,368 KB
testcase_27 AC 398 ms
142,508 KB
testcase_28 AC 140 ms
107,964 KB
testcase_29 AC 462 ms
153,300 KB
testcase_30 AC 208 ms
124,328 KB
testcase_31 AC 195 ms
119,540 KB
testcase_32 AC 390 ms
141,356 KB
testcase_33 AC 701 ms
197,088 KB
testcase_34 AC 216 ms
126,160 KB
testcase_35 AC 553 ms
174,808 KB
testcase_36 AC 33 ms
99,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pll;
vector<pll> G[4000100]; //weight,vertex
priority_queue<pll,vector<pll>,greater<pll>> que;
ll d[4000100],INF = 100000000000000000;
void add_edge(int from,int to,ll weight){ G[from].push_back({weight,to}); 
    // if(to==13) cout << from << " " << to << " " << weight << endl;
}
void dijkstra(int s){
    que.push({0,s});
    while(que.size()){
        pll p = que.top(); que.pop();
        ll dis = p.first,pos = p.second;
        if(d[pos]!=INF) continue;
        d[pos] = dis;
        for(auto e:G[pos]){
            if(d[e.second]==INF) que.push({dis + e.first,e.second});
        }
    }
}

int a[1010][1010];
int dx[8] = {1,1,0,-1,-1,-1,0,1};
int dy[8] = {0,1,1,1,0,-1,-1,-1};
bool ch(int h,int w,int nx,int ny){
    if(nx<0 || nx>=h || ny<0 || ny>=w || a[nx][ny]==-1) return false;
    return true;
}

int mp(int w,int nx,int ny){
    return w*nx + ny;
}

int main(){
    int i,j,k,h,w; cin >> h >> w;
    for(i=0;i<h - 2;i++){
        for(j=0;j<w;j++){
            cin >> a[i][j];
        }
    }
    for(i=0;i<h - 2;i++){
        for(j=0;j<w;j++){
            if(a[i][j]==-1) continue;
            for(k=0;k<8;k++){
                int nx = i + dx[k],ny = j + dy[k];
                if(!ch(h - 2,w,nx,ny)) continue;
                add_edge(mp(w,i,j),mp(w,nx,ny),a[i][j]);
            }
        }
    }
    int s = (h - 2)*w;
    int t = (h - 2)*w + 1;
    for(i=0;i<=t;i++) d[i] = INF;
    for(i=0;i<h - 2;i++){
        if(a[i][0]!=-1) add_edge(s,mp(w,i,0),0);
        if(a[i][w - 1]!=-1) add_edge(mp(w,i,w - 1),t,a[i][w - 1]);
    }
    dijkstra(s);
    if(d[t]!=INF) cout << d[t] << "\n";
    else cout << -1 << "\n";
    // for(i=0;i<(h - 2)*w;i++){
    //     cout << d[i] << " ";
    //     if(i%w==w - 1) cout << "\n";
    // }
    // cout << d[s] << " " << d[t] << endl;
}
0