結果

問題 No.845 最長の切符
ユーザー toyamatoyama
提出日時 2019-06-28 23:03:35
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 376 ms / 3,000 ms
コード長 1,038 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 29,224 KB
実行使用メモリ 34,696 KB
最終ジャッジ日時 2023-09-14 22:33:54
合計ジャッジ時間 3,217 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 0 ms
4,380 KB
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 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 0 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 54 ms
10,052 KB
testcase_16 AC 364 ms
34,552 KB
testcase_17 AC 52 ms
10,040 KB
testcase_18 AC 136 ms
18,228 KB
testcase_19 AC 20 ms
5,800 KB
testcase_20 AC 376 ms
34,696 KB
testcase_21 AC 313 ms
34,616 KB
testcase_22 AC 52 ms
9,984 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 361 ms
34,560 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 27 ms
20,344 KB
testcase_27 AC 0 ms
4,380 KB
testcase_28 AC 30 ms
20,552 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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 << 18][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] = max(graph[a][b], 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] &&
            !(s & (1 << i))) {

            ret = max(ret, dfs(i, s | (1 << i)) + graph[pos][i]);
        }
    }

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