結果

問題 No.845 最長の切符
ユーザー lotuslotus
提出日時 2019-06-28 23:02:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,895 ms / 3,000 ms
コード長 1,040 bytes
コンパイル時間 1,774 ms
コンパイル使用メモリ 172,108 KB
実行使用メモリ 21,068 KB
最終ジャッジ日時 2023-09-14 22:34:47
合計ジャッジ時間 9,374 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
20,864 KB
testcase_01 AC 20 ms
20,900 KB
testcase_02 AC 25 ms
20,852 KB
testcase_03 AC 14 ms
20,804 KB
testcase_04 AC 18 ms
20,816 KB
testcase_05 AC 15 ms
20,864 KB
testcase_06 AC 15 ms
20,740 KB
testcase_07 AC 15 ms
20,800 KB
testcase_08 AC 21 ms
20,804 KB
testcase_09 AC 19 ms
20,868 KB
testcase_10 AC 34 ms
20,760 KB
testcase_11 AC 22 ms
20,748 KB
testcase_12 AC 25 ms
21,068 KB
testcase_13 AC 19 ms
20,764 KB
testcase_14 AC 25 ms
20,948 KB
testcase_15 AC 133 ms
20,804 KB
testcase_16 AC 1,895 ms
20,820 KB
testcase_17 AC 395 ms
20,836 KB
testcase_18 AC 371 ms
20,844 KB
testcase_19 AC 101 ms
20,804 KB
testcase_20 AC 684 ms
20,836 KB
testcase_21 AC 489 ms
20,844 KB
testcase_22 AC 352 ms
20,808 KB
testcase_23 AC 91 ms
20,804 KB
testcase_24 AC 1,588 ms
20,876 KB
testcase_25 AC 9 ms
20,860 KB
testcase_26 AC 32 ms
21,064 KB
testcase_27 AC 12 ms
20,752 KB
testcase_28 AC 30 ms
20,852 KB
testcase_29 AC 9 ms
20,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
 
using namespace std;
 
#define INF 1000000007
#define LINF 1000000000000000007
 
typedef long long i64;
typedef pair<i64,i64> P;


int n, m;
vector<P> v[20];
bool used[20];
i64 dp[(1 << 17)][17];

i64 dfs(int now, i64 score, int was){
	if(dp[was][now] != 0) return dp[was][now];
	for(int i = 0; i < v[now].size(); i++){
		if(used[v[now][i].first]) continue;
		used[now] = 1;
		dp[was][now] = max(dp[was][now], dfs(v[now][i].first, score, was + (1 << v[now][i].first)) + v[now][i].second);
		used[v[now][i].first] = 0;
	}
	return dp[was][now];
}

int main(){
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		i64 a,b,c;
		cin >> a >> b >> c;
		v[a].push_back(P(b,c));
		v[b].push_back(P(a,c));
	}
	for(int i = 0; i < n; i++){
		sort(v[i].begin(), v[i].end(), greater<P>());
	}
	
	i64 ans = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < (1 << 17); j++) for(int k = 0; k < 17; k++){
			dp[j][k] = 0;
		}
		used[i] = 1;
		ans = max(ans, dfs(i, 0, (1 << i)));
		used[i] = 0;
	}
	cout << ans << endl;
	
	return 0;
}
0