結果

問題 No.845 最長の切符
ユーザー QCFiumQCFium
提出日時 2019-06-28 21:48:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 3,000 ms
コード長 1,082 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 174,644 KB
実行使用メモリ 7,768 KB
最終ジャッジ日時 2023-09-14 21:50:44
合計ジャッジ時間 3,389 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 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 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 14 ms
4,376 KB
testcase_16 AC 81 ms
7,572 KB
testcase_17 AC 18 ms
4,500 KB
testcase_18 AC 38 ms
5,368 KB
testcase_19 AC 8 ms
4,376 KB
testcase_20 AC 71 ms
7,592 KB
testcase_21 AC 41 ms
7,768 KB
testcase_22 AC 18 ms
4,384 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 81 ms
7,640 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 5 ms
7,528 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 6 ms
7,580 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int n = ri(), m = ri();
	int a[m], b[m], c[m];
	for (int i = 0; i < m; i++) a[i] = ri() - 1, b[i] = ri() - 1, c[i] = ri();
	std::map<int, int> hens[n];
	for (int i = 0; i < m; i++) {
		if (hens[a[i]].count(b[i])) hens[a[i]][b[i]] = std::max(hens[a[i]][b[i]], c[i]);
		else hens[a[i]][b[i]] = c[i];
		if (hens[b[i]].count(a[i])) hens[b[i]][a[i]] = std::max(hens[b[i]][a[i]], c[i]);
		else hens[b[i]][a[i]] = c[i];
	}
	int dp[1 << n][n];
	for (int i = 0; i < 1 << n; i++)
		for (int j = 0; j < n; j++) dp[i][j] = -1;
	for (int i = 0; i < n; i++) dp[1 << i][i] = 0;
	for (int i = 0; i < 1 << n; i++) {
		for (int j = 0; j < n; j++) {
			if (dp[i][j] < 0) continue;
			for (auto &k : hens[j]) {
				if (i >> k.first & 1) continue;
				dp[i | 1 << k.first][k.first] = std::max(dp[i | 1 << k.first][k.first], dp[i][j] + k.second);
			}
		}
	}
	int res = 0;
	for (int i = 0; i < 1 << n; i++)
	for (int j = 0; j < n; j++) res = std::max(res, dp[i][j]);
	std::cout << res << std::endl;
	return 0;
}

0