結果

問題 No.357 品物の並び替え (Middle)
ユーザー snrnsidysnrnsidy
提出日時 2021-08-15 16:28:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 640 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 198,616 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 19:05:07
合計ジャッジ時間 2,936 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 7 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int dp[1<<14];
vector <pair<int,int>> item[14];
int n,m,a,b,c;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> n >> m;

	for(int i=0;i<m;i++)
	{
		cin >> a >> b >> c;
		item[b].push_back(make_pair(a,c));
	}

	memset(dp,-1,sizeof(dp));
	dp[0] = 0;

	for(int i=0;i<(1<<n);i++)
	{
		if(dp[i]==-1) continue;
		for(int j=0;j<n;j++)
		{
			if((i&(1<<j))) continue;
			int score = 0;
			for(auto it : item[j])
			{
				if((i&(1 << it.first))) score += it.second;
			}
			dp[i + (1<<j)] = max(dp[i + (1<<j)],dp[i] + score);
		}
	}

	cout << dp[(1<<n)-1] << '\n';
	
	return 0;
}
0