結果

問題 No.357 品物の並び替え (Middle)
ユーザー amaridekinaiamaridekinai
提出日時 2019-09-17 18:13:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 831 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 144,428 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 19:46:36
合計ジャッジ時間 1,933 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void chmax(int &a, int b){ if( a < b){ swap(a,b);} return ;}
const int MAX_N = 1<<16;
int dp[MAX_N];

int pre[20][20];
int main(){
  int N,M; cin >> N >> M;
  
  for(int i = 0; i < 20; i++){
    for(int j = 0; j < 20; j++){
      pre[i][j] = 0;}}
  for(int  i =0; i  < MAX_N; i++){ dp[i] = 0;}
  
  for(int i = 0; i < M; i++){ 
    int a,b,score; cin >> a >> b >> score;
    pre[a][b] = score;}
  
  for(int mask = 0; mask < (1<<N); mask++){
    for(int a = 0; a < N; a++){
      if( !(mask >> a & 1)){
       int res = 0;
        
       for(int b = 0; b < N; b++){
         if( mask >> b & 1){  res += pre[a][b];}
       }
        
         chmax(dp[mask+(1<<a)], dp[mask]+res);
         }
    }
  }
        


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