結果

問題 No.2328 Build Walls
ユーザー KKT89KKT89
提出日時 2023-05-28 13:53:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,403 bytes
コンパイル時間 2,585 ms
コンパイル使用メモリ 215,820 KB
実行使用メモリ 13,528 KB
最終ジャッジ日時 2023-08-27 08:37:43
合計ジャッジ時間 9,055 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 36 ms
8,440 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 RE -
testcase_18 WA -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 30 ms
7,792 KB
testcase_22 WA -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
}

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];
        }
    }
//    vector<vector<vector<ll>>> dp(w, vector<vector<ll>> (h-2, vector<ll>(4, 1e18)));
    vector<vector<ll>> dp(w,vector<ll>(h-2,1e18));
//    auto f = [&](int i) -> int {
//        int res = 0;
//        if (i == 0) res |= 1;
//        if (i == h-3) res |= 2;
//        return res;
//    };
    for (int i = 0; i < h - 2; ++i) {
        if(a[i][0] != -1) dp[0][i] = a[i][0];
//        dp[0][i][f(i)]= a[i][0];
    }
    for (int i = 1; i < w; ++i) {
        for (int j = 0; j < h - 2; ++j) {
//            for (int k = 0; k < 4; ++k) {
//                if (a[j][i] != -1) {
//                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k]+a[j][i]);
//                }
//                if (j and a[j-1][i] != -1) {
//                    dp[i][j-1][k|f(j-1)] = min(dp[i][j-1][k|f(j-1)], dp[i-1][j][k]+a[j-1][i]);
//                }
//                if (j+1 < h-2 and a[j+1][i] != -1) {
//                    dp[i][j+1][k|f(j+1)] = min(dp[i][j+1][k|f(j+1)], dp[i-1][j][k]+a[j+1][i]);
//                }
//            }
            ll mi = dp[i-1][j];
            for (int k = -1; k <= 1; ++k) {
                if (0 <= j+k and j+k < h-2) {
                    mi = min(mi, dp[i-1][j+k]);
                }
            }
            if(a[j][i] != -1 and mi != 1e18) dp[i][j] = mi + a[j][i];
            if (j and a[j][i] != -1) dp[i][j] = min(dp[i][j], dp[i][j-1] + a[i][j]);
        }
        for (int j = h - 4; j >= 0; --j) {
            if (a[j][i] != -1) {
                dp[i][j] = min(dp[i][j], dp[i][j+1]+a[i][j]);
            }
        }
    }
    ll res = 1e18;
    for (int i = 0; i < h-2; ++i) {
        res = min(res, dp[w-1][i]);
    }
    if (res == 1e18) res = -1;
    cout << res << endl;
}
0