結果

問題 No.357 品物の並び替え (Middle)
ユーザー lapilapi
提出日時 2019-06-22 19:47:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,110 bytes
コンパイル時間 1,179 ms
コンパイル使用メモリ 107,284 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 03:04:49
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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

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

	return 0;
}
0