結果

問題 No.943 取り調べ
ユーザー Mister
提出日時 2020-04-26 13:00:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 139 ms / 1,206 ms
コード長 1,335 bytes
コンパイル時間 1,537 ms
コンパイル使用メモリ 79,884 KB
最終ジャッジ日時 2025-01-10 01:54:57
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

constexpr int INF = 1 << 30;

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> ss(n, 0);
    for (auto& s : ss) {
        for (int i = 0; i < n; ++i) {
            int x;
            std::cin >> x;
            s |= (x << i);
        }
    }

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    int ans = INF;
    std::vector<bool> ok(n);
    for (int b = 0; b < (1 << n); ++b) {
        int t = b;

        std::fill(ok.begin(), ok.end(), false);
        bool update = true;

        while (update) {
            update = false;
            for (int i = 0; i < n; ++i) {
                if (ok[i]) continue;

                auto s = ss[i];
                if ((t & s) == s) {
                    ok[i] = true;
                    update = true;
                    t |= (1 << i);
                }
            }
        }

        if (std::any_of(ok.begin(), ok.end(),
                        [](auto x) { return !x; })) continue;

        int cost = 0;
        for (int i = 0; i < n; ++i) {
            if ((b >> i) & 1) cost += xs[i];
        }

        ans = std::min(ans, cost);
    }

    std::cout << ans << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0