結果

問題 No.845 最長の切符
ユーザー kotamanegikotamanegi
提出日時 2019-06-28 21:54:29
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 1,570 ms / 3,000 ms
コード長 2,179 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 152,092 KB
実行使用メモリ 34,940 KB
最終ジャッジ日時 2024-11-30 14:12:38
合計ジャッジ時間 7,268 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
long long dp[17][(1 << 17)] = {};
long long path[17][17];
int main(){
#define int long long
	priority_queue<tuple<long long, long long, long long>, vector<tuple<long long, long long, long long>>, greater<tuple<long long, long long, long long>>> next;
	int n, m;
	cin >> n >> m;
	REP(i, m) {
		int a, b, c;
		cin >> a >> b >> c;
		a--;
		b--;
		path[a][b] = max(c, path[a][b]);
		path[b][a] = path[a][b];
	}
	REP(i, n) {
		next.push(make_tuple(0, i, (1 << i)));
	}
	while (next.empty() == false) {
		tuple<long long, long long, long long> now = next.top();
		next.pop();
		if (dp[get<1>(now)][get<2>(now)] == get<0>(now)) {
			//Marked as verifyed
			int now_here = get<1>(now);
			int donot_go = get<2>(now);
			int now_max = get<0>(now);
			for (int j = 0; j < n; ++j) {
				if (path[now_here][j] != 0) {
					//at least path exists
					if (((1 << j) & donot_go) == 0) {
						//ok to visit
						int next_max = now_max + path[now_here][j];
						int next_donotgo = donot_go + (1 << j);
						if (next_max > dp[j][next_donotgo]) {
							dp[j][next_donotgo] = next_max;
							next.push(make_tuple(next_max, j, next_donotgo));
						}
					}
				}
			}
		}
	}
	long long ans = 0;
	REP(i, 17) {
		REP(q, (1 << 17)) {
			ans = max(ans, dp[i][q]);
		}
	}
	cout << ans << endl;
}
0