結果

問題 No.845 最長の切符
ユーザー tottoripapertottoripaper
提出日時 2019-07-03 23:33:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 3,000 ms
コード長 1,132 bytes
コンパイル時間 1,462 ms
コンパイル使用メモリ 167,616 KB
実行使用メモリ 7,704 KB
最終ジャッジ日時 2023-10-17 07:49:49
合計ジャッジ時間 2,982 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,700 KB
testcase_01 AC 4 ms
7,700 KB
testcase_02 AC 4 ms
7,700 KB
testcase_03 AC 4 ms
7,700 KB
testcase_04 AC 4 ms
7,700 KB
testcase_05 AC 4 ms
7,700 KB
testcase_06 AC 4 ms
7,700 KB
testcase_07 AC 4 ms
7,700 KB
testcase_08 AC 4 ms
7,700 KB
testcase_09 AC 3 ms
7,700 KB
testcase_10 AC 4 ms
7,704 KB
testcase_11 AC 4 ms
7,704 KB
testcase_12 AC 4 ms
7,704 KB
testcase_13 AC 4 ms
7,700 KB
testcase_14 AC 4 ms
7,700 KB
testcase_15 AC 11 ms
7,704 KB
testcase_16 AC 41 ms
7,704 KB
testcase_17 AC 11 ms
7,704 KB
testcase_18 AC 20 ms
7,704 KB
testcase_19 AC 7 ms
7,704 KB
testcase_20 AC 40 ms
7,704 KB
testcase_21 AC 39 ms
7,704 KB
testcase_22 AC 10 ms
7,704 KB
testcase_23 AC 5 ms
7,704 KB
testcase_24 AC 39 ms
7,704 KB
testcase_25 AC 3 ms
7,700 KB
testcase_26 AC 39 ms
7,700 KB
testcase_27 AC 4 ms
7,700 KB
testcase_28 AC 39 ms
7,700 KB
testcase_29 AC 3 ms
7,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

int N, M;
int di[16][16], dp[1<<16][16];

int rec(int visited, int v){
    if(dp[visited][v] != -1){
        return dp[visited][v];
    }

    int mx = 0;

    for(int w=0;w<N;++w){
        if(di[v][w] == -1 || (visited >> w & 1)){
            continue;
        }
        
        mx = std::max(mx, rec(visited | (1 << w), w) + di[v][w]);
    }

    return dp[visited][v] = mx;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N >> M;

    std::fill(&di[0][0], &di[0][0] + 16 * 16, -1'000'000'000);

    for(int i=0;i<M;++i){
        int a, b, c;
        std::cin >> a >> b >> c;

        a -= 1;
        b -= 1;

        di[a][b] = std::max(di[a][b], c);
        di[b][a] = di[a][b];
    }

    memset(dp, -1, sizeof(dp));

    int mx = 0;

    for(int i=0;i<N;++i){
        mx = std::max(mx, rec(1 << i, i));
    }

    std::cout << mx << std::endl;
}
0