結果

問題 No.845 最長の切符
ユーザー Yang33
提出日時 2019-07-05 16:42:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 3,000 ms
コード長 829 bytes
コンパイル時間 1,764 ms
コンパイル使用メモリ 171,664 KB
実行使用メモリ 14,080 KB
最終ジャッジ日時 2024-09-21 21:44:50
合計ジャッジ時間 2,975 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i = s; i < t; i++)
using LL = long long;
using VL = vector<LL>;

int main() {

	int N, M; cin >> N >> M;
	const LL LINF = 1e18;
	vector<VL> cost(N, VL(N, -LINF));
	FOR(i, 0, M) {
		LL a, b, c; cin >> a >> b >> c;
		a--, b--;
		cost[a][b] = cost[b][a] = max(cost[b][a], c);
	}
	vector<VL> dp(1 << N, VL(N, -LINF));
	FOR(i, 0, N) {
		dp[1 << i][i] = 0;
	}
	LL ans = 0;
	FOR(state, 0, 1 << N) {
		FOR(i, 0, N) {
			if (state & 1 << i) {
				if (dp[state][i] == -LINF)continue;
				FOR(j, 0, N) {
					if (i == j)continue;
					if (state & 1 << j)continue;
					if (cost[i][j] ==- LINF)continue;
					int nx = state | (1 << j);
					dp[nx][j] = max(dp[nx][j], dp[state][i] + cost[i][j]);
					ans = max(ans, dp[nx][j]);
				}
			}
		}
	}
	cout << ans << endl;
}
0