結果

問題 No.357 品物の並び替え (Middle)
ユーザー Yuki InoueYuki Inoue
提出日時 2023-12-19 21:33:36
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 965 bytes
コンパイル時間 3,354 ms
コンパイル使用メモリ 249,288 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-19 21:33:41
合計ジャッジ時間 4,438 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll INF = 1e18;
ll MOD = 1e9 + 7;

int N, M;
int dp[1<<15];

bool contain(int mask, int pos){
    if((mask & (1<<pos)) != 0) return true;
    return false;
}

int main(){
    cin >> N >> M;
    vector<P> v(M);
    vector<int> score(M);
    
    rep(i,0,M) cin >> v[i].first >> v[i].second >> score[i];
    
    rep(i,0,1<<15) dp[i] = 0;

    rep(mask,1,1<<N) rep(i,0,N){
        if(contain(mask,i)){
            int sum = 0;
            rep(j,0,M){
                if(v[j].second == i && contain(mask, v[j].first)){
                    sum += score[j];
                }
                dp[mask] = max(dp[mask], dp[mask ^ (1<<i)] + sum);
            }
        }
    }

    cout << dp[(1<<N) - 1] << endl;
    return 0;
}
0