結果

問題 No.1284 Flip Game
ユーザー lorent_kyoprolorent_kyopro
提出日時 2020-11-08 15:57:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,342 bytes
コンパイル時間 2,599 ms
コンパイル使用メモリ 214,864 KB
実行使用メモリ 30,940 KB
最終ジャッジ日時 2023-09-29 21:13:09
合計ジャッジ時間 4,882 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 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,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 6 ms
4,680 KB
testcase_16 AC 7 ms
4,684 KB
testcase_17 AC 17 ms
10,052 KB
testcase_18 AC 16 ms
10,048 KB
testcase_19 AC 62 ms
30,584 KB
testcase_20 AC 22 ms
10,436 KB
testcase_21 AC 22 ms
10,452 KB
testcase_22 AC 22 ms
10,212 KB
testcase_23 AC 96 ms
30,764 KB
testcase_24 AC 92 ms
30,644 KB
testcase_25 AC 92 ms
30,940 KB
testcase_26 AC 63 ms
30,372 KB
testcase_27 AC 70 ms
30,292 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    cin >> n;
    auto c = make_vec<int>({n, n});
    rep(i, n) rep(j, n) cin >> c[i][j];

    auto dp = make_vec<ll>({1<<(2*n), n}, LLONG_MAX);
    using T = tuple<ll, int, int>;
    priority_queue<T, vector<T>, greater<T>> pq;
    rep(i, n) {
        dp[1<<i][i] = 0;
        pq.emplace(0, 1<<i, i);
    }
    while (!pq.empty()) {
        auto [cost, bit, i] = pq.top();
        pq.pop();
        if (dp[bit][i] < cost) continue;
        rep(j, n) {
            if (bit>>j & 1) continue;
            int nbit = bit | (1<<j);
            if (!(bit>>(i+n) & 1)) {
                nbit |= 1<<(i+n);
                nbit &= ~(1<<i);
            }
            ll ncost = cost + c[i][j];
            if (chmin(dp[nbit][j], ncost)) pq.emplace(ncost, nbit, j);
        }
    }

    ll ans = LLONG_MAX;
    rep(i, 1<<(2*n)) rep(j, n) {
        bool ok = true;
        rep(k, n) {
            if (i>>k & 1) continue;
            ok = false;
            break;
        }
        if (ok) chmin(ans, dp[i][j]);
    }

    cout << ans << '\n';
}
0