結果

問題 No.845 最長の切符
ユーザー Y17Y17
提出日時 2019-06-28 22:17:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 492 ms / 3,000 ms
コード長 929 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 148,044 KB
実行使用メモリ 19,264 KB
最終ジャッジ日時 2023-09-14 22:07:23
合計ジャッジ時間 4,332 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
18,980 KB
testcase_01 AC 8 ms
18,964 KB
testcase_02 AC 7 ms
19,024 KB
testcase_03 AC 7 ms
19,032 KB
testcase_04 AC 7 ms
19,032 KB
testcase_05 AC 8 ms
18,972 KB
testcase_06 AC 8 ms
19,020 KB
testcase_07 AC 7 ms
19,124 KB
testcase_08 AC 7 ms
18,972 KB
testcase_09 AC 7 ms
18,984 KB
testcase_10 AC 8 ms
19,264 KB
testcase_11 AC 7 ms
18,972 KB
testcase_12 AC 7 ms
18,980 KB
testcase_13 AC 8 ms
19,000 KB
testcase_14 AC 7 ms
19,020 KB
testcase_15 AC 26 ms
19,020 KB
testcase_16 AC 492 ms
19,104 KB
testcase_17 AC 116 ms
19,100 KB
testcase_18 AC 82 ms
19,020 KB
testcase_19 AC 25 ms
18,996 KB
testcase_20 AC 130 ms
19,048 KB
testcase_21 AC 81 ms
18,980 KB
testcase_22 AC 101 ms
19,088 KB
testcase_23 AC 27 ms
19,128 KB
testcase_24 AC 410 ms
19,128 KB
testcase_25 AC 7 ms
19,012 KB
testcase_26 AC 7 ms
18,976 KB
testcase_27 AC 7 ms
18,972 KB
testcase_28 AC 7 ms
19,128 KB
testcase_29 AC 7 ms
18,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
int n, m;

vector<pair<int, int> > graph[20];

int memo[20][100010];

int dp(int idx, int bit){
    if(memo[idx][bit] != -1) return memo[idx][bit];

    int ans = 0;

    for(int i = 0;i < graph[idx].size();i++){
        int next = graph[idx][i].first;
        int ncost = graph[idx][i].second;
        if((bit & (1 << next)) == 0){
            ans = max(ans, dp(next, bit | (1 << next)) + ncost);
        }
    }

    return memo[idx][bit] = ans;
}

signed main(){
    cin >> n >> m;

    int a, b, c;
    for(int i = 0;i < m;i++){
        cin >> a >> b >> c;
        a--;
        b--;
        graph[a].push_back(make_pair(b, c));
        graph[b].push_back(make_pair(a, c));
    }

    int ans = 0;
    memset(memo, -1, sizeof(memo));

    for(int i = 0;i < n;i++){
        ans = max(ans, dp(i, (1 << i)));
    }

    cout << ans << endl;

    return 0;
}


0