結果

問題 No.845 最長の切符
ユーザー finefine
提出日時 2019-06-28 23:07:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 3,000 ms
コード長 2,389 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 170,900 KB
実行使用メモリ 11,476 KB
最終ジャッジ日時 2023-09-14 22:35:42
合計ジャッジ時間 3,165 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 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 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 13 ms
5,268 KB
testcase_16 AC 48 ms
11,476 KB
testcase_17 AC 11 ms
5,232 KB
testcase_18 AC 24 ms
7,228 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 55 ms
11,284 KB
testcase_21 AC 63 ms
11,240 KB
testcase_22 AC 12 ms
5,436 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 48 ms
11,300 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 7 ms
4,384 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 9 ms
4,864 KB
testcase_29 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

ll dp[16][1 << 16];
bool memo[16][1 << 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;
        memo[i][1 << i] = true;
    }

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

    ll ans = 0;
    for (int i = 1; i < (1 << n) - 1; i++) {
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                if (!memo[j][i]) continue;
                for (int k = 0; k < n; k++) {
                    if (i & (1 << k)) continue;
                    if (d[j][k] == -1) continue;
                    dp[k][i | (1 << k)] = max(dp[k][i | (1 << k)], dp[j][i] + d[j][k]);
                    memo[k][i | (1 << k)] = true;
                    ans = max(ans, dp[k][i | (1 << k)]);
                }
            }
        }
    }

    /* 
    ll ans = 0;
    do {
        ll hoge = 0;
        for (int i = 1; i < n; i++) {
            if (d[v[i-1]][v[i]] == -1) {
                sort(v.begin() + i + 1, v.end());
                reverse(v.begin() + i + 1, v.end());
                break;
            } else {
                hoge += d[v[i-1]][v[i]];
            }
        }
        ans = max(ans, hoge);
    } while (next_permutation(v.begin(), v.end()));
    cout << ans << "\n";
    */

    /* 
    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