結果

問題 No.357 品物の並び替え (Middle)
ユーザー なおなお
提出日時 2016-04-02 02:36:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,129 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 160,360 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 07:37:50
合計ジャッジ時間 2,035 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))

int n, m;
int k[20][20];
int dp[(1<<14)-1];

int dfs(int bit){
    if(dp[bit] >= 0) return dp[bit];

    int res = 0;
    // すでに使用済みの数字がビットマップ[bit]のとき、
    // まだ使われてない数字をつけてみる
    REP(i,n){
        if((bit>>i)&1) continue; // 使用済み

        int bit2 = bit | (1<<i);
        // i をつけたと仮定し、後ろをいろいろ変化させたときの最大を取得
        int v = dfs(bit2);
        // i をつけたときの得点を加算(iより後ろの未使用数字の並びはどうであっても得点は同じ)
        REP(j,n){
            if(i == j) continue;
            if((bit>>j)&1) continue; // 使用済み
            v += k[i][j];
        }
        res = max(res, v);
    }

    return (dp[bit] = res);
}

int main(){
    cin >> n >> m;

    REP(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        k[a][b] = c;
    }
    memset(dp,-1,sizeof(dp));

    int res = dfs(0);
    cout << res << endl;

    return 0;
}
0