結果

問題 No.357 品物の並び替え (Middle)
ユーザー CleyLCleyL
提出日時 2022-01-16 00:13:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 886 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 67,200 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 23:15:32
合計ジャッジ時間 1,470 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;


int A[15][15];
int dp[15][(1<<15)];

int main(){
  int n,m;cin>>n>>m;
  for(int i = 0; m > i; i++){
    int i1,i2,s;cin>>i1>>i2>>s;
    A[i1][i2] = s;
  }
  for(int i = 0; n > i; i++){
    for(int j = 0; (1<<n) > j; j++){
      if(__builtin_popcount(j) != i)continue;
      //cout << j << " (";
      for(int k = 0; n > k; k++){
        if((j&(1<<k)) == 0){
          int add = 0;
          for(int l = 0; n > l; l++){
            if((j&(1<<l)) != 0){
              add += A[l][k];
            }
          }
          dp[i+1][j+(1<<k)] = max(dp[i][j]+add,dp[i+1][j+(1<<k)]);
        }
      }
      //cout << ") ";
    }
    //cout << endl;
  }
  // for(int i = 0; n > i; i++){
  //   for(int j = 0; (1<<n) > j; j++){
  //     cout << dp[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  cout << dp[n][(1<<n)-1] << endl;
}
0