結果

問題 No.357 品物の並び替え (Middle)
ユーザー pianonekopianoneko
提出日時 2019-03-12 18:12:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 840 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 165,232 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 20:22:56
合計ジャッジ時間 2,396 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define llINF ((long long)1e18)
#define iINF ((int)1e9)
#define ALL(obj) obj.begin(), obj.end()

using namespace std;

int N, M;
int item1[200], item2[200], score[200], dp[1 << 14];

int rec(int S) {
    if (dp[S] != -1) {
        return dp[S];
    }

    if (S == (1 << N) - 1) {
        return 0;
    }

    int res = 0;
    REP(s, N) {
        int plus = 0;
        if ((S >> s) & 1) continue;
        REP(i, M) {
            if (((S >> item1[i]) & 1) && item2[i] == s) {
                plus += score[i];
            }
        }
        res = max(res, rec(S | (1 << s)) + plus);
    }
    return dp[S] = res;
}

int main() {
    cin >> N >> M;
    REP(i, M) { cin >> item1[i] >> item2[i] >> score[i]; }
    memset(dp, -1, sizeof(dp));
    cout << rec(0) << endl;
}
0