結果
問題 | No.845 最長の切符 |
ユーザー |
![]() |
提出日時 | 2019-06-28 22:04:37 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 47 ms / 3,000 ms |
コード長 | 1,155 bytes |
コンパイル時間 | 1,948 ms |
コンパイル使用メモリ | 194,736 KB |
最終ジャッジ日時 | 2025-01-07 05:32:09 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <bits/stdc++.h> #define rep(i, a, n) for(int i = a; i < n; i++) #define int long long using namespace std; typedef pair<int, int> P; const int mod = 1000000007; const int INF = 1e18; int d[20][20]; int dp[1 << 16][20]; signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; rep(i, 0, n) rep(j, 0, n) d[i][j] = -1; rep(i, 0, m){ int a, b, c; cin >> a >> b >> c; a--; b--; d[a][b] = max(d[a][b], c); d[b][a] = max(d[b][a], c); } rep(i, 0, 1 << n) rep(j, 0, n) dp[i][j] = -1; rep(i, 0, n) dp[1 << i][i] = 0; for(int mask = 0; mask < (1LL << n); mask++){ for(int i = 0; i < n; i++){ if(dp[mask][i] == -1) continue; for(int j = 0; j < n; j++){ if(mask & (1LL << j)) continue; if(d[i][j] == -1) continue; dp[mask | (1LL << j)][j] = max(dp[mask | (1LL << j)][j], dp[mask][i] + d[i][j]); } } } int ans = 0; rep(i, 0, 1 << n){ rep(j, 0, n){ ans = max(ans, dp[i][j]); } } cout << ans << endl; }