結果

問題 No.845 最長の切符
ユーザー 👑 jupirojupiro
提出日時 2020-01-24 18:04:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 3,000 ms
コード長 1,646 bytes
コンパイル時間 1,079 ms
コンパイル使用メモリ 118,396 KB
実行使用メモリ 14,004 KB
最終ジャッジ日時 2023-10-12 04:14:00
合計ジャッジ時間 3,643 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 18 ms
5,516 KB
testcase_16 AC 95 ms
13,952 KB
testcase_17 AC 17 ms
5,648 KB
testcase_18 AC 41 ms
7,840 KB
testcase_19 AC 7 ms
4,348 KB
testcase_20 AC 103 ms
13,784 KB
testcase_21 AC 97 ms
13,720 KB
testcase_22 AC 16 ms
5,536 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 94 ms
13,876 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 14 ms
14,004 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 14 ms
13,760 KB
testcase_29 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

ll N, M;
vector<vector<ll>> g;
vector<vector<ll>> dp;
ll dfs(ll S, ll pre) {
	if (S == (1 << N) - 1) return 0;
	if (pre != -1 && dp[S][pre] != -1) return dp[S][pre];
	ll res = 0;
	if (pre == -1) {
		for (ll i = 0; i < N; i++) chmax(res, dfs(1 << i, i));
		return res;
	}
	else {
		for (ll i = 0; i < N; i++) {
			if (!(S >> i & 1) && g[pre][i]) chmax(res, dfs(S + (1 << i), i) + g[pre][i]);
		}
		return dp[S][pre] = res;
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%lld %lld", &N, &M);
	g = vector<vector<ll>>(N, vector<ll>(N));
	dp = vector<vector<ll>>(1 << N, vector<ll>(N, -1));
	for (ll i = 0; i < M; i++) {
		ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c);
		a--; b--;
		chmax(g[a][b], c);
		chmax(g[b][a], c);
	}

	cout << dfs(0, -1) << endl;
	return 0;
}
0