結果

問題 No.845 最長の切符
ユーザー betrue12betrue12
提出日時 2019-06-28 21:47:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 3,000 ms
コード長 890 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 166,048 KB
実行使用メモリ 7,640 KB
最終ジャッジ日時 2023-09-14 21:49:46
合計ジャッジ時間 3,578 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

void chmax(int& a, int b){
    a = max(a, b);
}

int nth_bit(int64_t num, int n){
    return (num >> n) & 1;
}

int main(){

    int N, M;
    cin >> N >> M;
    int dist[16][16] = {0};
    for(int i=0; i<M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        chmax(dist[a][b], c);
        chmax(dist[b][a], c);
    }

    int dp[1<<16][16];
    int B = 1<<N;
    for(int b=0; b<B; b++) for(int i=0; i<N; i++) dp[b][i] = -1e9;
    for(int i=0; i<N; i++) dp[1<<i][i] = 0;
    for(int b=0; b<B; b++) for(int i=0; i<N; i++) if(nth_bit(b, i)){
        for(int j=0; j<N; j++) if(!nth_bit(b, j) && dist[i][j] > 0){
            chmax(dp[b|(1<<j)][j], dp[b][i] + dist[i][j]);
        }
    }

    int ans = 0;
    for(int b=0; b<B; b++) for(int i=0; i<N; i++) chmax(ans, dp[b][i]);
    cout << ans << endl;
    return 0;
}
0