結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 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; }

const int INF = 1e18;

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;
	}

	for (int mask = 0; mask < (1<<N)-1; mask++) {
		for (int i = 0; i < N; i++) { // iを選ぶ
			int score = 0;
			if (mask & (1 << i)) continue;
			for (int j = 0; j < N; j++) {
				if (mask & (1 << j)) {
					score += item[j][i];
				}
			}
			chmax(dp[mask | (1<<i)], dp[mask] + score);
		}
	}

	cout << dp[(1<<N)-1] << endl;

	return 0;
}
0