結果

問題 No.845 最長の切符
ユーザー toyamatoyama
提出日時 2019-06-28 22:53:38
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,096 bytes
コンパイル時間 1,203 ms
コンパイル使用メモリ 28,912 KB
実行使用メモリ 18,284 KB
最終ジャッジ日時 2023-09-14 22:29:29
合計ジャッジ時間 4,028 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 RE -
testcase_21 RE -
testcase_22 WA -
testcase_23 WA -
testcase_24 RE -
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 31 ms
18,192 KB
testcase_27 AC 0 ms
4,380 KB
testcase_28 RE -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#define max(X, Y) ((X) > (Y) ? (X) : (Y))
typedef long long ll;
//struct edge {ll cost, ll to; };

const ll INF = 1ll << 60ll;
const ll MINF = -1 * (1ll << 60ll);

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

ll n, m;
ll graph[32][32];
ll a, b, c;
ll ans;

ll dfs(ll pos, ll s);

int main()
{
    scanf("%lld %lld", &n, &m);

    for (int i = 0; i < m; i++) {
        scanf("%lld %lld %lld", &a, &b, &c);

        graph[a][b] = graph[b][a] = c;
    }

    for (int i = 1; i <= n; i++) {

        for (int i = 1 << n; i >= 0; i--) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = 0;
            }
        }
        
        ans = max(ans, dfs(i, 1 << i));
    }

    printf("%lld\n", ans);
}

ll dfs(ll pos, ll s)
{
    if (dp[s][pos] ) {
        return dp[s][pos];
    }
    
    ll ret = 0;
    for (int i = 1; i <= n; i++) {
        if (graph[pos][i]) {
            ll to = i;
            ll cost = graph[pos][i];

            if (!(s & (1 << to))) {
                ret = max(ret, dfs(to, s | (1 << to)) + cost);
            }
        }
    }

    return dp[s][pos] = ret;
}
0