結果

問題 No.2328 Build Walls
ユーザー aradarad
提出日時 2023-05-28 18:56:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 234 ms / 3,000 ms
コード長 1,885 bytes
コンパイル時間 5,301 ms
コンパイル使用メモリ 266,348 KB
実行使用メモリ 13,536 KB
最終ジャッジ日時 2023-08-27 15:11:11
合計ジャッジ時間 9,963 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 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 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 66 ms
8,808 KB
testcase_14 AC 65 ms
6,060 KB
testcase_15 AC 58 ms
6,024 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 65 ms
6,692 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 57 ms
8,216 KB
testcase_22 AC 97 ms
7,496 KB
testcase_23 AC 228 ms
13,028 KB
testcase_24 AC 219 ms
13,380 KB
testcase_25 AC 226 ms
13,116 KB
testcase_26 AC 194 ms
13,168 KB
testcase_27 AC 213 ms
13,344 KB
testcase_28 AC 114 ms
13,224 KB
testcase_29 AC 227 ms
13,124 KB
testcase_30 AC 123 ms
13,512 KB
testcase_31 AC 121 ms
13,356 KB
testcase_32 AC 210 ms
13,196 KB
testcase_33 AC 234 ms
13,136 KB
testcase_34 AC 126 ms
13,536 KB
testcase_35 AC 231 ms
13,268 KB
testcase_36 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using P = pair<ll,ll>;
using VP = vector<P>;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define out(x) cout << x << endl
#define dout(x) cout << fixed << setprecision(10) << x << endl
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)(x.size())
#define re0 return 0
#define pcnt __builtin_popcountll
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int inf = 1e9;
constexpr ll INF = 1e18;
//using mint = modint1000000007;
using mint = modint998244353;
int di[8] = {1,0,-1,0,1,1,-1,-1};
int dj[8] = {0,1,0,-1,1,-1,1,-1};

int main(){
    ll h,w;
    cin >> h >> w;
    h -= 2;
    VVL a(h,VL(w));
    rep(i,h)rep(j,w) cin >> a[i][j];
    VVL dist(h,VL(w,INF));
    priority_queue<P,VP,greater<P>> pq;
    rep(i,h){
        if(a[i][0] == -1) continue;
        dist[i][0] = a[i][0];
        pq.emplace(dist[i][0],i*w+0);
    }
    while(!pq.empty()){
        auto [d,v] = pq.top();
        pq.pop();
        ll i = v/w;
        ll j = v%w;
        if(d > dist[i][j]) continue;
        rep(v,8){
            int ni = i+di[v];
            int nj = j+dj[v];
            if(ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
            if(a[ni][nj] == -1) continue;
            if(chmin(dist[ni][nj],dist[i][j]+a[ni][nj])){
                pq.emplace(dist[ni][nj],ni*w+nj);
            }
        }
    }
    ll ans = INF;
    rep(i,h) chmin(ans,dist[i][w-1]);
    if(ans == INF) ans = -1;
    out(ans);
}
0