結果

問題 No.357 品物の並び替え (Middle)
ユーザー okkuukenkenokkuukenken
提出日時 2019-04-06 10:45:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 905 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 51,432 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 21:29:16
合計ジャッジ時間 1,226 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX_N = 14, MAX_M = MAX_N * (MAX_N - 1);

int G[MAX_N][MAX_N];

void add_edge(int from, int to, int cost) {
	G[from][to] = cost;
}

int N, M;
int item1[MAX_M], item2[MAX_M], score[MAX_M];

int dp[1 << MAX_N];

void solve() {
	sizeof(G, 0, sizeof(G));
	sizeof(dp, -1, sizeof(dp));

	for (int i = 0; i < M; i++) {
		add_edge(item1[i], item2[i], score[i]);
	}

	for (int S = 0; S < 1 << N; S++) {
		for (int t = 0; t < N; t++) {
			if (!(S >> t & 1)) {
				int s = 0;
				for (int f = 0; f < N; f++) {
					if (S >> f & 1) {
						s += G[f][t];
					}
				}
				dp[S | 1 << t] = max(dp[S | 1 << t], dp[S] + s);
			}
		}
	}

	printf("%d\n", dp[(1 << N) - 1]);
}

int main() {
	scanf("%d %d", &N, &M);
	for (int i = 0; i < M; i++) {
		scanf("%d %d %d", &item1[i], &item2[i], &score[i]);
	}

	solve();
}
0