結果

問題 No.845 最長の切符
コンテスト
ユーザー MarcusAureliusAntoninus
提出日時 2019-06-28 22:27:08
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 46 ms / 3,000 ms
コード長 919 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,273 ms
コンパイル使用メモリ 215,048 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2026-06-07 12:57:55
合計ジャッジ時間 2,738 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

int main()
{
	int N, M;
	scanf("%d%d", &N, &M);
	using vi = std::vector<int64_t>;
	using vvi = std::vector<vi>;

	vvi graph(N, vi(N));
	for (int i{}; i < M; i++)
	{
		int a, b;
		int64_t c;
		scanf("%d%d%lld", &a, &b, &c);
		a--; b--;
		graph[a][b] = std::max(graph[a][b], c);
		graph[b][a] = std::max(graph[b][a], c);
	}
	vvi dp(1 << N, vi(N, -1));
	for (int i{}; i < N; i++)
		dp[1 << i][i] = 0;
	int64_t max{};
	for (int set_i{1}; set_i < (1 << N); set_i++)
	{
		for (int now_i{}; now_i < N; now_i++)
		{
			max = std::max(max, dp[set_i][now_i]);
			if ((~set_i >> now_i & 1) || dp[set_i][now_i] < 0)
				continue;
			for (int next_i{}; next_i < N; next_i++)
				if ((~set_i >> next_i & 1) && graph[now_i][next_i] > 0)
					dp[set_i | (1 << next_i)][next_i] = std::max(dp[set_i | (1 << next_i)][next_i], dp[set_i][now_i] + graph[now_i][next_i]);
		}
	}
	printf("%lld\n", max);

	return 0;
}
0