結果
問題 | No.357 品物の並び替え (Middle) |
ユーザー | pekempey |
提出日時 | 2016-04-01 23:09:20 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 8 ms / 5,000 ms |
コード長 | 766 bytes |
コンパイル時間 | 1,406 ms |
コンパイル使用メモリ | 164,472 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-02 09:18:58 |
合計ジャッジ時間 | 2,143 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
#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; }