結果

問題 No.2328 Build Walls
ユーザー KKT89KKT89
提出日時 2023-05-28 13:55:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,403 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 214,856 KB
実行使用メモリ 13,364 KB
最終ジャッジ日時 2023-08-27 08:40:35
合計ジャッジ時間 5,207 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 36 ms
8,320 KB
testcase_14 WA -
testcase_15 AC 19 ms
5,620 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 6 ms
4,380 KB
testcase_20 WA -
testcase_21 AC 29 ms
7,772 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 57 ms
13,064 KB
testcase_29 WA -
testcase_30 AC 66 ms
13,180 KB
testcase_31 AC 66 ms
12,996 KB
testcase_32 WA -
testcase_33 AC 62 ms
13,016 KB
testcase_34 AC 69 ms
12,996 KB
testcase_35 AC 68 ms
13,004 KB
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[j][i]);
        }
        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[j][i]);
            }
        }
    }
    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