結果

問題 No.845 最長の切符
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2019-06-28 22:27:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 3,000 ms
コード長 919 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 202,920 KB
実行使用メモリ 14,132 KB
最終ジャッジ日時 2023-09-14 22:10:47
合計ジャッジ時間 3,538 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 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 1 ms
4,380 KB
testcase_15 AC 12 ms
5,600 KB
testcase_16 AC 43 ms
13,848 KB
testcase_17 AC 10 ms
5,588 KB
testcase_18 AC 22 ms
7,832 KB
testcase_19 AC 5 ms
4,436 KB
testcase_20 AC 51 ms
14,132 KB
testcase_21 AC 60 ms
13,960 KB
testcase_22 AC 10 ms
5,588 KB
testcase_23 AC 4 ms
4,384 KB
testcase_24 AC 43 ms
13,920 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 17 ms
13,796 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 18 ms
13,964 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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