結果

問題 No.357 品物の並び替え (Middle)
ユーザー lapilapi
提出日時 2019-06-22 19:45:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,104 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 106,740 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-27 03:04:29
合計ジャッジ時間 2,361 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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<<60

int 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[1 << nxt], dp[i] + addval);
			}
		}
	}

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

	return 0;
}
0