結果

問題 No.90 品物の並び替え
ユーザー nominomi
提出日時 2019-03-11 22:36:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 901 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 165,000 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 20:17:23
合計ジャッジ時間 2,044 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }

int N, M;
int item[100][100];
int dp[1<<9];

int rec(int mask) {
	if (mask >= (1<<N)-1) return 0;
	if (dp[mask] != -1) return dp[mask];

	int ret = 0;
	for (int i = 0; i < N; i++) { // iは新しく選ぶやつ
		// iを選んだことがあるなら飛ばす
		if (mask & (1 << i)) continue;

		// 新しく選ぶやつに関するスコアを計算
		int score = 0;
		for (int j = 0; j < N; j++) {
			if (mask & (1 << j)) {
				score += item[j][i];
			}
		}

		chmax(ret, rec(mask | (1 << i)) + score);
	}

	return dp[mask] = ret;
}

signed main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		item[a][b] = c;
	}

	fill(dp, dp+(1<<9), -1);

	int ans = rec(0);

	cout << ans << endl;

	return 0;
}
0