結果

問題 No.845 最長の切符
ユーザー tarattata1tarattata1
提出日時 2019-06-29 14:18:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 3,000 ms
コード長 1,760 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 74,216 KB
実行使用メモリ 11,200 KB
最終ジャッジ日時 2023-09-14 23:13:06
合計ジャッジ時間 2,115 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 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 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 15 ms
7,096 KB
testcase_16 AC 56 ms
11,132 KB
testcase_17 AC 14 ms
7,084 KB
testcase_18 AC 27 ms
9,036 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 58 ms
11,200 KB
testcase_21 AC 60 ms
11,188 KB
testcase_22 AC 14 ms
7,040 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 56 ms
11,124 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 20 ms
11,188 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 21 ms
11,176 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

ll dp[1<<16][16];

int main(int argc, char* argv[])
{
    int n,m;
    scanf("%d%d", &n, &m);

    int g[16][16]={0};
    int i,j;
    for(i=0; i<16; i++) {
        for(j=0; j<16; j++) {
            g[i][j]=INF;
        }
    }
    for(i=0; i<m; i++) {
        int a,b,c;
        scanf("%d%d%d", &a, &b, &c);
        g[a-1][b-1]=(g[a-1][b-1]!=INF? MAX(g[a-1][b-1],c): c);
        g[b-1][a-1]=g[a-1][b-1];
    }

    int k;
    for(i=0; i<(1<<n); i++) {
        for(k=0; k<n; k++) {
            dp[i][k] = -1;
        }
    }

    for(i=1; i<(1<<n); i++) {
        for(k=0; k<n; k++) {
            if(i & (1<<k)) {
                int prev = (i & ~(1<<k));
                if(prev==0) {
                    dp[i][k] = 0;
                }
                else {
                    int j;
                    for(j=0; j<n; j++) {
                        if(dp[prev][j]>=0 && g[j][k]!=INF) {
                            dp[i][k] = MAX(dp[i][k], dp[prev][j] + g[j][k]);
                        }
                    }
                }
            }
        }
    }
    ll ans=0;
    for(i=1; i<(1<<n); i++) {
        for(k=0; k<n; k++) {
            if(dp[i][k]>=0) {
                ans = MAX(ans, dp[i][k]);
            }
        }
    }
    printf("%lld\n", ans);

    return 0;
}
0