結果

問題 No.845 最長の切符
ユーザー ahe100ahe100
提出日時 2019-06-29 00:25:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 845 ms / 3,000 ms
コード長 1,021 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 164,832 KB
実行使用メモリ 134,608 KB
最終ジャッジ日時 2023-09-14 22:51:12
合計ジャッジ時間 9,653 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
134,416 KB
testcase_01 AC 65 ms
134,436 KB
testcase_02 AC 66 ms
134,416 KB
testcase_03 AC 64 ms
134,484 KB
testcase_04 AC 65 ms
134,584 KB
testcase_05 AC 65 ms
134,456 KB
testcase_06 AC 64 ms
134,460 KB
testcase_07 AC 67 ms
134,444 KB
testcase_08 AC 38 ms
134,496 KB
testcase_09 AC 36 ms
134,420 KB
testcase_10 AC 41 ms
134,476 KB
testcase_11 AC 37 ms
134,380 KB
testcase_12 AC 38 ms
134,480 KB
testcase_13 AC 36 ms
134,460 KB
testcase_14 AC 38 ms
134,468 KB
testcase_15 AC 139 ms
134,472 KB
testcase_16 AC 825 ms
134,380 KB
testcase_17 AC 140 ms
134,428 KB
testcase_18 AC 332 ms
134,584 KB
testcase_19 AC 73 ms
134,476 KB
testcase_20 AC 827 ms
134,608 KB
testcase_21 AC 816 ms
134,420 KB
testcase_22 AC 133 ms
134,600 KB
testcase_23 AC 49 ms
134,424 KB
testcase_24 AC 826 ms
134,404 KB
testcase_25 AC 36 ms
134,412 KB
testcase_26 AC 822 ms
134,400 KB
testcase_27 AC 36 ms
134,404 KB
testcase_28 AC 845 ms
134,412 KB
testcase_29 AC 36 ms
134,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;

/*
AC
グラフ上の最長経路問題
典型中の典型だが、いざ書こうとすると迷う
*/

ll n,m,dp[1<<16][16][16],ans=-1e18;
ll dist[16][16];

ll f(ll s,ll p,ll q){
	if(dp[s][p][q]!=-1e18)return dp[s][p][q];
	ll sum=0;
	rep(i,n){
		if(!(1 & s>>i)){
			sum=max(sum,f(s+(1<<i),i,q)+dist[p][i]);  // 片方だけで十分であることに気付いた
		}
	}
	return dp[s][p][q]=sum;
}

void init(){
	cin>>n>>m;
	rep(i,n)rep(j,n)dist[i][j]=-1e18;
	rep(i,m){
		ll a,b,c;
		cin>>a>>b>>c;
		a--;b--;
		dist[a][b]=max(dist[a][b],c);
		dist[b][a]=max(dist[b][a],c);
	}
	rep(i,1<<16)rep(j,16)rep(k,16)dp[i][j][k]=-1e18;
}

int main(void){
	init();
	rep(i,n){
		ans=max(ans,f(1<<i,i,i));
	}
	cout<<ans<<endl;
	return 0;
}
0