結果

問題 No.357 品物の並び替え (Middle)
ユーザー snrnsidysnrnsidy
提出日時 2021-08-15 16:28:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 640 bytes
コンパイル時間 2,047 ms
コンパイル使用メモリ 196,340 KB
最終ジャッジ日時 2025-01-23 22:27:11
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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