結果

問題 No.357 品物の並び替え (Middle)
コンテスト
ユーザー pekempey
提出日時 2016-04-01 23:09:20
言語 C++11
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 6 ms / 5,000 ms
+ 258µs
コード長 766 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 950 ms
コンパイル使用メモリ 181,244 KB
実行使用メモリ 9,972 KB
最終ジャッジ日時 2026-09-08 19:45:42
合計ジャッジ時間 2,333 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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