結果

問題 No.357 品物の並び替え (Middle)
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-19 07:33:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,066 bytes
コンパイル時間 411 ms
コンパイル使用メモリ 56,108 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 16:46:02
合計ジャッジ時間 970 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

const int kMAX_N = 16;

int score[kMAX_N][kMAX_N];

int total_score[1 << kMAX_N][kMAX_N];
int dp[1 << kMAX_N];

int main() {
    int N, M;
    cin >> N >> M;

    for (int i = 0; i < M; i++) {
        int item1, item2;
        cin >> item1 >> item2;
        cin >> score[item1][item2];
    }

    for (int mask = 0; mask < (1 << N); mask++) {
        for (int item = 0; item < N; item++) {
            int getscore = 0;
            for (int have_item = 0; have_item < N; have_item++) {
                if (!((mask >> have_item) & 1)) continue;
                getscore += score[have_item][item];
            }
            total_score[mask][item] = getscore;
        }
    }

    for (int mask = 0; mask < (1 << N); mask++) {
        for (int item = 0; item < N; item++) {
            if (!((mask >> item) & 1)) continue;
            int prev_mask = mask & ~(1 << item);
            dp[mask] = max(dp[mask], dp[prev_mask] + total_score[prev_mask][item]);
        }
    }
    cout << dp[(1 << N) - 1] << endl;
    return 0;
}
0