結果

問題 No.357 品物の並び替え (Middle)
ユーザー xyz600600xyz600600
提出日時 2016-04-02 09:28:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 862 bytes
コンパイル時間 528 ms
コンパイル使用メモリ 65,568 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-10 08:13:58
合計ジャッジ時間 1,286 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
  int N, M;
  cin >> N >> M;

  vector<vector<int>> table;
  for( int i = 0; i < N; i++ )
  {
	table.push_back( vector<int>( N ) );
  }

  for( int i = 0; i < M; i++ )
  {
	int f, t, v;
	cin >> f >> t >> v;
	table[ f ][ t ] = v;
  }

  const int S = 1 << N;

  // best[使ってる集合]
  vector<int> best( S );

  fill( best.begin(), best.end(), 0 );
  
  for( int i = 0; i < S - 1; i++ )
  {
	for( int j = 0; j < N; j++ ) // 今から追加する桁
	{
	  if ( !( i & ( 1 << j ) ) )
	  {
		int sub_score = 0;
		for( int k = 0; k < N; k++ ) // 既にある桁
		{
		  if ( i & ( 1 << k ) )
		  {
			sub_score += table[ k ][ j ];
		  }
		}
		best[ i + (1 << j) ] = max( best[ i + (1 << j) ], best[ i ] + sub_score );
	  }
	}
  }

  cout << best[ S - 1 ] << endl;
}
0