結果

問題 No.845 最長の切符
ユーザー fine
提出日時 2019-06-28 22:41:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,373 bytes
コンパイル時間 1,609 ms
コンパイル使用メモリ 172,328 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-07-02 05:00:35
合計ジャッジ時間 6,401 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, ll>;

ll dp[16][16][1 << 16];
//bool memo[16][16][1 << 16];
vector<P> g[16];

void dfs(int src, int cur, int used, ll& ans) {
    //memo[src][cur][used] = true;
    for (P& p : g[cur]) {
        if (used & (1 << p.first)) continue;
        int n_used = (used | (1 << p.first));
        ll& tar = dp[src][p.first][n_used];
        ll cost = dp[src][cur][used] + p.second;
        if (tar < cost) {
            tar = cost;
            ans = max(ans, tar);
            dfs(src, p.first, n_used, ans);
        }
    }

    for (P& p : g[src]) {
        if (used & (1 << p.first)) continue;
        int n_used = (used | (1 << p.first));
        ll& tar = dp[cur][p.first][n_used];
        ll cost = dp[cur][src][used] + p.second;
        if (tar < cost) {
            tar = cost;
            ans = max(ans, tar);
            dfs(cur, p.first, n_used, ans);
        }
    }    
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--; b--;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        dfs(i, i, 1 << i, ans);
    }
    cout << ans << endl;
    return 0;
}
0