結果

問題 No.1345 Beautiful BINGO
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-01-16 14:17:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,960 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 214,112 KB
実行使用メモリ 137,288 KB
最終ジャッジ日時 2024-05-05 16:33:26
合計ジャッジ時間 7,161 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 7 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 877 ms
9,752 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
using namespace std;
using ll = long long;
template<typename T>
inline bool chmax(T& a, const T& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T>
inline bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

int main() {
    int n, m;
    cin >> n >> m;
    auto a = make_vec<int>({n, n});
    rep(i, n) rep(j, n) cin >> a[i][j];

    vector<ll> sum(n);
    rep(i, n) rep(j, n) sum[i] += a[i][j];

    auto cost = make_vec<ll>({n, 1 << (n + 2)}, 0);
    rep(i, n) rep(bit, 1 << (n + 2)) {
        vector<bool> used(n);
        rep(j, n) if (bit >> j & 1) cost[i][bit] += a[i][j], used[j] = true;
        if ((bit >> n & 1) && !used[i]) cost[i][bit] += a[i][i], used[i] = true;
        if ((bit >> (n + 1) & 1) && !used[n - 1 - i]) cost[i][bit] += a[i][n - 1 - i], used[n - 1 - i] = true;
    }

    auto dp = make_vec<ll>({1 << (n + 2), n + 2}, LLONG_MAX);

    rep(bit, 1 << n) {
        ll now = 0;
        rep(j, n) {
            if (bit >> j & 1) now += a[0][j];
        }
        chmin(dp[bit | ((bit & 1) << n) | (bit >> (n - 1) & 1) << (n + 1)][bit == (1 << n) - 1], now);
    }

    for (int i = 1; i < n; ++i) {
        auto ndp = make_vec<ll>({1 << (n + 2), n + 2}, LLONG_MAX);
        for (int bit = 0; bit < 1 << (n + 2); ++bit) {
            for (int x = 0; x <= n; ++x) {
                if (dp[bit][x] == LLONG_MAX) continue;
                for (int nbit = bit; nbit >= 0; --nbit) {
                    nbit &= bit;
                    int nx = x + (sum[i] == cost[i][nbit]);
                    chmin(ndp[nbit][nx], dp[bit][x] + cost[i][nbit]);
                }
                chmin(ndp[bit][x + 1], dp[bit][x] + sum[i]);
            }
        }
        dp = move(ndp);
    }

    ll ans = LLONG_MAX;
    rep(bit, 1 << (n + 2)) rep(x, n + 1) {
        int y = x + __builtin_popcount(bit);
        if (y >= m) chmin(ans, dp[bit][x]);
    }
    cout << ans << '\n';
}
0