結果

問題 No.845 最長の切符
ユーザー finefine
提出日時 2019-06-28 22:51:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,347 bytes
コンパイル時間 1,676 ms
コンパイル使用メモリ 172,504 KB
実行使用メモリ 82,812 KB
最終ジャッジ日時 2023-09-14 22:29:00
合計ジャッジ時間 7,049 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 12 ms
5,472 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 4 ms
4,492 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 468 ms
21,832 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector< vector<ll> > d(n, vector<ll>(n, -1));
    for (int i = 0; i < n; i++) {
        d[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--; b--;
        d[a][b] = max(c, d[a][b]);
        d[b][a] = max(c, d[b][a]);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (d[i][j] == -1) continue;
            g[i].emplace_back(j, d[i][j]);
            g[j].emplace_back(i, d[j][i]);
        }
    }

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