結果

問題 No.357 品物の並び替え (Middle)
ユーザー amaridekinaiamaridekinai
提出日時 2019-09-19 15:56:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 854 bytes
コンパイル時間 2,630 ms
コンパイル使用メモリ 144,772 KB
実行使用メモリ 11,724 KB
最終ジャッジ日時 2023-09-26 12:53:31
合計ジャッジ時間 2,734 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,564 KB
testcase_01 AC 4 ms
11,604 KB
testcase_02 AC 4 ms
11,460 KB
testcase_03 AC 5 ms
11,492 KB
testcase_04 AC 4 ms
11,492 KB
testcase_05 AC 4 ms
11,488 KB
testcase_06 AC 4 ms
11,496 KB
testcase_07 AC 5 ms
11,496 KB
testcase_08 AC 5 ms
11,452 KB
testcase_09 AC 8 ms
11,516 KB
testcase_10 AC 6 ms
11,604 KB
testcase_11 AC 5 ms
11,724 KB
testcase_12 AC 13 ms
11,636 KB
testcase_13 AC 9 ms
11,548 KB
testcase_14 AC 9 ms
11,516 KB
testcase_15 AC 5 ms
11,560 KB
testcase_16 AC 6 ms
11,564 KB
testcase_17 AC 5 ms
11,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll MAX_N = 20;

ll dp[1<<MAX_N];

ll pre[20][20];

void chmax(ll &a, ll b){ if( a < b){ swap(a,b);} return ;}

int main(){ 
  ll N,M; cin >> N >> M;
  
  for(ll i = 0; i < 1<<MAX_N; i++){ dp[i] = 0;}
  
  for(ll i = 0; i < 20; i++){
    for(ll j = 0; j < 20; j++){ 
      pre[i][j] = 0;}}
  
  for(ll i = 0; i < M; i++){
    ll a,b,score; cin >> a >> b >> score; 
    pre[a][b] = score;}
 
  for(ll k = 0; k < 1<<N; k++){
    for(ll i = 0; i < N; i++){ 
      if(!((k>>i)&1)){ //まだi番目の品物を並べていない
        ll res = 0;
        for(ll j = 0; j < N; j++){ 
          if( i != j && ((k >> j) & 1)){
            res += pre[i][j];
        }
      }
       chmax(dp[k+(1<<i)],dp[k]+res);
    }
   }
  }
  
  cout << dp[(1<<N)-1] << endl; return 0;}
        
    
0