結果

問題 No.2328 Build Walls
ユーザー KKT89KKT89
提出日時 2023-05-28 14:02:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 379 ms / 3,000 ms
コード長 2,322 bytes
コンパイル時間 2,812 ms
コンパイル使用メモリ 225,624 KB
実行使用メモリ 122,112 KB
最終ジャッジ日時 2023-08-27 08:52:14
合計ジャッジ時間 8,627 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 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,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 73 ms
28,260 KB
testcase_14 AC 97 ms
31,820 KB
testcase_15 AC 76 ms
23,016 KB
testcase_16 AC 13 ms
6,364 KB
testcase_17 AC 77 ms
21,976 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 9 ms
5,632 KB
testcase_20 AC 8 ms
5,004 KB
testcase_21 AC 36 ms
18,224 KB
testcase_22 AC 152 ms
46,868 KB
testcase_23 AC 295 ms
78,520 KB
testcase_24 AC 277 ms
71,556 KB
testcase_25 AC 292 ms
77,032 KB
testcase_26 AC 229 ms
58,588 KB
testcase_27 AC 267 ms
67,900 KB
testcase_28 AC 61 ms
33,136 KB
testcase_29 AC 297 ms
78,488 KB
testcase_30 AC 134 ms
49,624 KB
testcase_31 AC 114 ms
44,760 KB
testcase_32 AC 262 ms
66,664 KB
testcase_33 AC 379 ms
122,112 KB
testcase_34 AC 139 ms
51,364 KB
testcase_35 AC 341 ms
99,600 KB
testcase_36 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

template<typename T>
struct Dijkstra{
    const T inf=numeric_limits<T>::max();
    using P=pair<T,int>;
    int n;
    vector<vector<pair<int,T>>> g;
    vector<T> d;
    Dijkstra(int n):n(n),g(n),d(n){}
    void add_edge(int u,int v,T w){
        g[u].emplace_back(v,w);
    }
    vector<T> build(int s){
        for(int i=0;i<n;i++){
            d[i]=inf;
        }
        d[s]=0;
        priority_queue<P,vector<P>,greater<P>> pq;
        pq.emplace(d[s],s);
        while(pq.size()){
            P p=pq.top(); pq.pop();
            int v=p.second;
            if(d[v]<p.first)continue;
            for(auto &e:g[v]){
                int u=e.first; T c=e.second;
                if(d[u]>d[v]+c){
                    d[u]=d[v]+c;
                    pq.emplace(d[u],u);
                }
            }
        }
        return d;
    }
};

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

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h,w; cin >> h >> w;
    vector<vector<ll>> a(h-2,vector<ll>(w));
    for (int i = 0; i < h - 2; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> a[i][j];
        }
    }
    h -= 2;
    Dijkstra<ll> g(h*w+2);
    int S = h*w, T = h*w+1;
    for (int i = 0; i < h; ++i) {
        if (a[i][0] != -1) g.add_edge(S, i*w+0, a[i][0]);
        if (a[i][w-1] != -1) g.add_edge(i*w+w-1, T, 0);
    }
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (a[i][j] == -1) continue;
            for (int k = 0; k < 8; ++k) {
                int nx = i+dx[k], ny = j+dy[k];
                if (0 <= nx and nx < h and 0 <= ny and ny < w and a[nx][ny] != -1) {
                    g.add_edge(i*w+j, nx*w+ny, a[nx][ny]);
                }
            }
        }
    }

    ll res = g.build(S)[T];
    if (res == numeric_limits<ll>::max()) res = -1;
    cout << res << endl;
}
0