結果

問題 No.845 最長の切符
ユーザー gazellegazelle
提出日時 2019-06-28 23:10:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 3,000 ms
コード長 2,601 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 208,392 KB
実行使用メモリ 13,916 KB
最終ジャッジ日時 2023-09-14 22:37:10
合計ジャッジ時間 3,963 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 14 ms
5,412 KB
testcase_16 AC 55 ms
13,856 KB
testcase_17 AC 13 ms
5,432 KB
testcase_18 AC 26 ms
8,104 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 60 ms
13,848 KB
testcase_21 AC 71 ms
13,812 KB
testcase_22 AC 13 ms
5,408 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 55 ms
13,844 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 58 ms
13,788 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 60 ms
13,916 KB
testcase_29 AC 1 ms
4,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define FOR(i, n, m) for(long long i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = std::int_fast64_t;
using ld = long double;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-15;
template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
	os << to_string(p.first) << " " << to_string(p.second);
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
	REP(i, v.size()) {
		if(i) os << " ";
		os << to_string(v[i]);
	}
	return os;
}

/*
template<typename T>
struct Treap {
	double drand() { // random number in [0, 1]
		static random_device rd;
		static mt19937 mt(rd());
		return (unsigned)mt() / (double)numeric_limits<unsigned>::max();
	}
	T v;
	double p;
	int cnt;
	Treap* lch;
	Treap* rch;
	Treap(T v) : v(v), p(drand()), cnt(1), lch(NULL), rch(NULL) { }
	Treap* update() {
		this->size = size(this->lch) + size(this->rch) + 1;
		return this;
	}
	static int size(Treap* t) {
		if(!t) return 0;
		else return t->cnt;
	}
	static Treap* merge(Treap* l, Treap* r) {
		if(!l || !r) {
			if(!l) return r;
			else return l;
		}
		if(l->p >= r->p) {
			l->rch = merge(l->rch, r);
			return l->update();
		} else {
			r->lch = merge(r->lch, l);
			return r->update();
		}
	}
	static pair<Treap*, Treap*> split(Treap* t, int k) {
		// split [0, k) and [k, n)
		if(k == 0) return {NULL, t};
		if(!(t->l)) {
			auto tmp = split(t->r, k - 1);
			t->r = tmp.first;
			return {t->update(), tmp.second};
		} else if(!(t->r)) {
			auto tmp = split(t->r, k - 1);
			t->r = tmp.first;
			return {t->update(), tmp.second};
		} else {

		}
	}
	Treap* insert() {
	}
	Treap* erase() {
	}
	T operator[](int k) {
	}
};
*/

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<vector<int>> g(n, vector<int>(n, -1));
	REP(i, m) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		g[a][b] = max(g[a][b], c);
		g[b][a] = max(g[b][a], c);
	}
	vector<vector<ll>> dp((1 << n), vector<ll>(n, -1));
	REP(i, n) {
		dp[1 << i][i] = 0;
	}
	ll ans = 0;
	REP(bit, 1 << n) REP(j, n) {
		bitset<16> bi(bit);
		if(bi.count() <= 1) continue;
		if(!bi[j]) continue;
		bi[j] = 0;
		REP(k, n) {
			if(!bi[k]) continue;
			if(g[k][j] == -1) continue;
			if(dp[bi.to_ullong()][k] != -1) dp[bit][j] = max(dp[bit][j], dp[bi.to_ullong()][k] + g[k][j]);
		}
		ans = max(ans, dp[bit][j]);
	}
	cout << ans << endl;
	return 0;
}
// ---------------------------------------
0