結果

問題 No.357 品物の並び替え (Middle)
ユーザー HachimoriHachimori
提出日時 2016-04-02 12:46:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,034 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 55,944 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-10 08:17:50
合計ジャッジ時間 1,257 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,948 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstring>
using namespace std;
const int NODE = 14;
const int BUF = 1 << NODE;


int nNode;
int adj[NODE][NODE];

void read() {
    memset(adj, 0, sizeof(adj));

    int nEdge;
    cin >> nNode >> nEdge;
    
    for (int i = 0; i < nEdge; ++i) {
        int s, t, c;
        cin >> s >> t >> c;
        adj[s][t] = c;
    }
}


int rec(int mask, int dp[BUF]) {
    
    if (mask == 1 << nNode) {
        return 0;
    }
    
    int &ret = dp[mask];
    if (ret >= 0) return ret;
    
    ret = 0;
    
    for (int i = 0; i < nNode; ++i) {
        if (mask & (1 << i)) continue;
        
        int toAdd = 0;
        for (int j = 0; j < nNode; ++j) {
            if ((mask & (1 << j))) {
                toAdd += adj[j][i];
            }
        }
        
        ret = max(ret, rec(mask | (1 << i), dp) + toAdd);
    }
    
    return ret;
}


void work() {
    int dp[BUF];
    memset(dp, -1, sizeof(dp));
    
    cout << rec(0, dp) << endl;
}


int main() {
    read();
    work();
    return 0;
}
0