結果

問題 No.845 最長の切符
コンテスト
ユーザー Tatsu_mr
提出日時 2026-01-14 13:07:59
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 331 ms / 3,000 ms
コード長 886 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,997 ms
コンパイル使用メモリ 344,644 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2026-01-14 13:08:06
合計ジャッジ時間 6,243 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<pair<int, int>>> g(N);
    rep(i, M) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    vector<vector<int>> dp((1 << N), vector<int>(N, -1));
    rep(v, N) {
        dp[1 << v][v] = 0;
    }
    rep(s, 1 << N) {
        rep(v, N) {
            if (dp[s][v] == -1) { continue; }
            for (auto [nv, d] : g[v]) {
                if (s & (1 << nv)) { continue; }
                int ns = s | (1 << nv);
                dp[ns][nv] = max(dp[ns][nv], dp[s][v] + d);
            }
        }
    }
    int ans = 0;
    rep(i, 1 << N) {
        ans = max(ans, *max_element(dp[i].begin(), dp[i].end()));
    }
    cout << ans << "\n";
}
0