結果
問題 | No.357 品物の並び替え (Middle) |
ユーザー |
![]() |
提出日時 | 2019-06-22 19:47:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 8 ms / 5,000 ms |
コード長 | 1,110 bytes |
コンパイル時間 | 1,546 ms |
コンパイル使用メモリ | 106,796 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-26 09:45:25 |
合計ジャッジ時間 | 2,359 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
#include <iostream>#include <string>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <list>#include <set>#include <map>#include <numeric>#include <regex>#include <tuple>#include<iomanip>using namespace std;typedef long long ll;typedef pair<int, int> P;#define MOD 1000000007 // 10^9 + 7#define INF 1000000000 // 10^9#define LLINF 1LL<<60int dp[1 << 14];vector<vector<P>> V(14);int main() {cin.tie(0);ios::sync_with_stdio(false);int N, M; cin >> N >> M;for (int i = 0; i < M; i++) {int a, b, val; cin >> a >> b >> val;V[b].push_back(P(a, val)); // bより前にaがあれば価値が+val}for (int i = 0; i < (1 << N); i++) {for (int nxt = 0; nxt < N; nxt++) {if (!(1 & (i >> nxt))) { // nxtが未使用ならint addval = 0; //後ろにnxtを加えることで生じる追加価値for (int k = 0; k < V[nxt].size(); k++) {if (1 & (i >> V[nxt][k].first)) addval += V[nxt][k].second;}dp[i + (1 << nxt)] = max(dp[i + (1 << nxt)], dp[i] + addval);}}}cout << dp[(1 << N) - 1] << endl;return 0;}