結果

問題 No.357 品物の並び替え (Middle)
ユーザー tsukiotsukio
提出日時 2016-05-10 01:11:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 880 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 69,944 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 15:01:21
合計ジャッジ時間 1,632 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 1 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 3 ms
5,376 KB
testcase_10 AC 2 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 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<vector<pair<int, int>>> scores;
vector<int> dp;
int mask;

int solve(int bit, int n) {
	if ((bit ^ mask) == 0) {
		return 0;
	}

	if (dp[bit] != -1) {
		return dp[bit];
	}
	dp[bit] = 0;

	for (int i = 0; i < n; i++) {
		if ((bit >> i) & 1) continue;
		int sum = 0;
		for (const auto& pair : scores[i]) {
			if ((bit >> pair.first) & 1) {
				sum += pair.second;
			}
		}
		dp[bit] = max(dp[bit], sum + solve(bit | 1 << i, n));
	}

	return dp[bit];
}

int main() {
	int n, m;
	cin >> n >> m;
	
	scores.resize(n);
	for (int i = 0; i < m; i++) {
		int item1, item2, score;
		cin >> item1 >> item2 >> score;
		scores[item1].emplace_back(item2, score);
	}
	
	dp.resize(1 << n, -1);

	mask = 0;
	for (int i = 0; i < n; i++) {
		mask = mask << i | 1;
	}

	cout << solve(0, n) << endl;

	return 0;
}
0