結果
| 問題 | 
                            No.519 アイドルユニット
                             | 
                    
| コンテスト | |
| ユーザー | 
                             ふーらくたる
                         | 
                    
| 提出日時 | 2017-05-29 00:00:08 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,712 bytes | 
| コンパイル時間 | 826 ms | 
| コンパイル使用メモリ | 96,528 KB | 
| 実行使用メモリ | 244,204 KB | 
| 最終ジャッジ日時 | 2024-09-21 16:03:20 | 
| 合計ジャッジ時間 | 5,319 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | TLE * 1 -- * 33 | 
ソースコード
#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;
}
            
            
            
        
            
ふーらくたる