結果

問題 No.845 最長の切符
ユーザー ok
提出日時 2019-06-29 00:01:14
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 55 ms / 3,000 ms
コード長 1,158 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 73,264 KB
実行使用メモリ 20,384 KB
最終ジャッジ日時 2024-07-02 05:15:53
合計ジャッジ時間 1,920 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

#define MAX 20

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N, M;
	int a, b, c;
	int ans = 0;
	static int cost[MAX][MAX];
	static int dp[MAX][(1<<17)];

	cin>>N>>M;
	
	for(int i = 0; i < M; i++){
		cin>>a>>b>>c;
		a--, b--;
		cost[a][b] = max(cost[a][b], c);
		cost[b][a] = max(cost[b][a], c);
	}
	
	for(int i = 1; i < (1<<N); i++){
		for(int j = 0; j < N; j++){
			if(!(i&(1<<j))) continue;
			int bit = i&(~(1<<j));
			int maximum = 0;
			for(int k = 0; k < N; k++){
				if(!(bit&(1<<k))) continue;
				if(!cost[k][j]) continue;
				// if(dp[k][bit] <= 0) continue;
				
				maximum  = max(maximum, dp[k][bit] + cost[k][j]);
			}
			dp[j][i] = maximum;
			ans = max(maximum, ans);
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0