結果

問題 No.943 取り調べ
ユーザー finefine
提出日時 2019-12-06 12:58:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 1,206 ms
コード長 1,008 bytes
コンパイル時間 2,734 ms
コンパイル使用メモリ 170,780 KB
実行使用メモリ 5,188 KB
最終ジャッジ日時 2023-08-24 19:50:51
合計ジャッジ時間 3,744 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 193 ms
5,100 KB
testcase_05 AC 171 ms
5,188 KB
testcase_06 AC 172 ms
5,164 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 193 ms
5,188 KB
testcase_09 AC 46 ms
4,380 KB
testcase_10 AC 47 ms
4,376 KB
testcase_11 AC 23 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 52 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 51 ms
4,380 KB
testcase_23 AC 183 ms
5,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector< vector<int> > x(n, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> x[i][j];
        }
    }
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<ll> dp(1 << n, 1e15);
    dp[0] = 0;
    for (int i = 0; i < (1 << n) - 1; i++) {
        for (int j = 0; j < n; ++j) {
            if (i & (1 << j)) continue;
            int next_mask = (i | (1 << j));
            ll cost = 0;
            for (int k = 0; k < n; ++k) {
                if (i & (1 << k)) continue;
                if (x[j][k]) {
                    cost += a[k];
                    next_mask |= (1 << k);
                }
            }
            dp[next_mask] = min(dp[next_mask], dp[i] + cost);
        }
    }
    cout << dp[(1 << n) - 1] << "\n";
    return 0;
}
0