結果

問題 No.357 品物の並び替え (Middle)
ユーザー albicillaalbicilla
提出日時 2017-08-14 19:07:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 673 bytes
コンパイル時間 2,404 ms
コンパイル使用メモリ 161,232 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 11:26:36
合計ジャッジ時間 2,753 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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


bool contain(int s,int i){
  if((s&(1<<i))!=0)return true;
  return false;
}
int dp[(1<<15)];
int main(){
  int N,M;
  cin>>N>>M;
  vector<pair<int,int>> vec(M);
  vector<int> score(M);

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

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