結果

問題 No.519 アイドルユニット
ユーザー ふーらくたるふーらくたる
提出日時 2017-05-29 00:00:08
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,712 bytes
コンパイル時間 1,596 ms
コンパイル使用メモリ 95,308 KB
実行使用メモリ 239,500 KB
最終ジャッジ日時 2023-10-21 14:49:26
合計ジャッジ時間 5,877 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <functional>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <vector>
#include <array>
#include <tuple>
#include <utility>
#include <numeric>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <assert.h>
#include <cstdlib>
#include <list>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {
    return s << "(" << p.first << ", " << p.second << ")";
}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
    s << "[";
    for (int i = 0; i < v.size(); i++) s << (i == 0 ? "" : ", ") << v[i];
    s << "]";
    return s;
}

#define ALL(a) (a).begin(), (a).end()

int size[1 << 24], dp[1 << 24], cost2[1 << 24], cost4[1 << 24];
int table[24][24];

int main() {
    int n;
    cin >> n;
    for (int r = 0; r < n; r++) {
        for (int c = 0; c < n; c++) {
            cin >> table[r][c];
        }
    }
    for (int mask = 0; mask < (1 << n); mask++) {
        for (int i = 0; i < n; i++) {
            if ((mask >> i) & 1) size[mask]++;
        }
    }

    vector<PII> foo2, foo4;
    for (int a = 0; a < n; a++) {
        for (int b = a + 1; b < n; b++) {
            int mask = (1 << a) | (1 << b);
            cost2[mask] = table[a][b];
            foo2.push_back(make_pair(mask, cost2[mask]));
        }
    }

    for (int a = 0; a < n; a++) {
        for (int b = a + 1; b < n; b++) {
            for (int c = b + 1; c < n; c++) {
                for (int d = c + 1; d < n; d++) {
                    int mask = (1 << a) | (1 << b) | (1 << c) | (1 << d);
                    cost4[mask] = max(table[a][b] + table[c][d], max(table[a][c] + table[b][d],
                                                                     table[a][d] + table[b][c]));
                    foo4.push_back(make_pair(mask, cost4[mask]));
                }
            }
        }
    }

    memset(dp, -1, sizeof(dp));
    if (n % 4 == 0) {
        dp[(1 << n) - 1] = 0;
    } else {
        for (PII& f2 : foo2) {
            dp[((1 << n) - 1) ^ f2.first] = f2.second;
        }
    }
    for (int mask = (1 << n) - 1; mask > 0; mask--) {
        if (dp[mask] < 0 || size[mask] % 4 != 0) continue;

        for (PII& f4 : foo4) {
            if ((mask & f4.first) == f4.first) {
                dp[mask ^ f4.first] = max(dp[mask ^ f4.first], dp[mask] + f4.second);
            }
        }
    }
    cout << dp[0] << endl;

    return 0;
}
0