結果

問題 No.357 品物の並び替え (Middle)
ユーザー pekempeypekempey
提出日時 2016-04-01 23:09:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 766 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 164,948 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 07:35:07
合計ジャッジ時間 2,243 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 7 ms
6,944 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

int main() {
	int n, m;
	cin >> n >> m;
	
	vector<vector<pair<int, int>>> item(n);
	for (int i = 0; i < m; i++) {
		int u, v, s;
		cin >> u >> v >> s;
		item[v].emplace_back(u, s);
	}

	static long long dp[1 << 14];
	fill_n(dp, 1 << 14, -1e9);
	dp[0] = 0;

	for (int i = 0; i < 1 << n; i++) {
		for (int j = 0; j < n; j++) if (~i & 1 << j) {
			long long gain = 0;
			for (auto p : item[j]) {
				if (~i & 1 << p.first) continue;
				gain += p.second;
			}
			chmax(dp[i | 1 << j], dp[i] + gain);
		}
	}

	cout << dp[(1 << n) - 1] << endl;
}
0