結果

問題 No.845 最長の切符
ユーザー snrnsidysnrnsidy
提出日時 2021-06-23 00:18:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 378 ms / 3,000 ms
コード長 990 bytes
コンパイル時間 2,018 ms
コンパイル使用メモリ 202,688 KB
実行使用メモリ 7,756 KB
最終ジャッジ日時 2023-09-06 00:46:08
合計ジャッジ時間 4,665 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,536 KB
testcase_01 AC 3 ms
7,480 KB
testcase_02 AC 3 ms
7,540 KB
testcase_03 AC 4 ms
7,608 KB
testcase_04 AC 3 ms
7,596 KB
testcase_05 AC 4 ms
7,616 KB
testcase_06 AC 3 ms
7,744 KB
testcase_07 AC 4 ms
7,472 KB
testcase_08 AC 4 ms
7,484 KB
testcase_09 AC 4 ms
7,696 KB
testcase_10 AC 4 ms
7,528 KB
testcase_11 AC 4 ms
7,544 KB
testcase_12 AC 4 ms
7,476 KB
testcase_13 AC 4 ms
7,468 KB
testcase_14 AC 4 ms
7,492 KB
testcase_15 AC 17 ms
7,464 KB
testcase_16 AC 378 ms
7,756 KB
testcase_17 AC 93 ms
7,672 KB
testcase_18 AC 58 ms
7,480 KB
testcase_19 AC 18 ms
7,520 KB
testcase_20 AC 83 ms
7,620 KB
testcase_21 AC 43 ms
7,472 KB
testcase_22 AC 80 ms
7,484 KB
testcase_23 AC 20 ms
7,572 KB
testcase_24 AC 310 ms
7,560 KB
testcase_25 AC 3 ms
7,468 KB
testcase_26 AC 5 ms
7,612 KB
testcase_27 AC 3 ms
7,544 KB
testcase_28 AC 5 ms
7,552 KB
testcase_29 AC 4 ms
7,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int dp[1 << 16][16];
vector <pair<int, int>> adj[16];
int n, m, a, b, c;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		cin >> a >> b >> c;
		a -= 1;
		b -= 1;
		adj[a].push_back(make_pair(b, c));
		adj[b].push_back(make_pair(a, c));
	}

	for (int i = 0; i < (1 << 16); i++)
	{
		for (int j = 0; j < 16; j++)
		{
			dp[i][j] = -1e9;
		}
	}

	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] <= -1e9) continue;
			for (auto it : adj[j])
			{
				int k = it.first;
				int cost = it.second;
				if ((i & (1 << k))) continue;
				int mask = i + (1 << k);
				dp[mask][k] = max(dp[mask][k], dp[i][j] + cost);
			}
		}
	}

	int res = 0;

	for (int i = 0; i < (1 << n); i++)
	{
		for (int j = 0; j < n; j++)
		{
			res = max(res, dp[i][j]);
		}
	}

	cout << res << '\n';

	return 0;
}
0