結果

問題 No.1308 ジャンプビーコン
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-01-07 21:34:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,869 ms / 4,000 ms
コード長 2,295 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 468,588 KB
最終ジャッジ日時 2024-01-07 21:34:48
合計ジャッジ時間 23,342 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,600 KB
testcase_01 AC 41 ms
55,600 KB
testcase_02 AC 43 ms
55,600 KB
testcase_03 AC 54 ms
61,576 KB
testcase_04 AC 49 ms
61,576 KB
testcase_05 AC 56 ms
61,444 KB
testcase_06 AC 45 ms
61,444 KB
testcase_07 AC 46 ms
61,444 KB
testcase_08 AC 84 ms
76,620 KB
testcase_09 AC 75 ms
73,304 KB
testcase_10 AC 73 ms
73,428 KB
testcase_11 AC 71 ms
73,072 KB
testcase_12 AC 72 ms
73,032 KB
testcase_13 AC 132 ms
78,348 KB
testcase_14 AC 120 ms
78,464 KB
testcase_15 AC 119 ms
78,204 KB
testcase_16 AC 123 ms
78,076 KB
testcase_17 AC 134 ms
77,044 KB
testcase_18 AC 685 ms
83,176 KB
testcase_19 AC 722 ms
83,436 KB
testcase_20 AC 709 ms
82,416 KB
testcase_21 AC 605 ms
80,368 KB
testcase_22 AC 596 ms
80,620 KB
testcase_23 AC 41 ms
55,600 KB
testcase_24 AC 79 ms
78,100 KB
testcase_25 AC 85 ms
78,100 KB
testcase_26 AC 591 ms
81,252 KB
testcase_27 AC 616 ms
81,636 KB
testcase_28 AC 579 ms
81,508 KB
testcase_29 AC 1,608 ms
224,228 KB
testcase_30 AC 1,539 ms
223,724 KB
testcase_31 AC 3,869 ms
468,588 KB
testcase_32 AC 1,759 ms
227,048 KB
testcase_33 AC 1,752 ms
223,844 KB
testcase_34 AC 631 ms
81,252 KB
testcase_35 AC 610 ms
81,384 KB
testcase_36 AC 469 ms
82,028 KB
testcase_37 AC 461 ms
81,132 KB
testcase_38 AC 894 ms
86,628 KB
testcase_39 AC 798 ms
85,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1308

from collections import deque

def main():
    N, Q, C = map(int, input().split())
    next_nodes = [[] for _ in range(N)]
    edges = {}
    for _ in range(N-1):
        a, b, l = map(int, input().split())
        next_nodes[a-1].append((b - 1, l))
        next_nodes[b-1].append((a - 1, l))
        edges[(a - 1, b - 1)] = l
        edges[(b - 1, a - 1)] = l

    
    X = list(map(int, input().split()))
    path_array = []
    for i in range(Q - 1):
        s_x = X[i] - 1
        e_x = X[i + 1] - 1
        queue = deque()
        queue.append(s_x)
        parents = [-2] * N
        parents[s_x] = -1
        while len (queue) > 0:
            v = queue.popleft()
            for next_node, _ in next_nodes[v]:
                if next_node == parents[v]:
                    continue

                parents[next_node] = v
                queue.append(next_node)
        
        path = []
        v = e_x
        while v != s_x:
            path.append(v)
            v = parents[v]
        path.append(s_x)
        path.reverse()
        path_array.append(path)

    # 下手に走った時の走行時間の累積
    cum_dist_array = []
    cum_dist = 0
    for path in path_array:
        cum_dists = []
        prev_p = -1
        for p in path:
            if prev_p != -1:
                cum_dist += edges[(prev_p, p)]
            cum_dists.append(cum_dist)
            prev_p = p
        cum_dist_array.append(cum_dists)
    
    dp = [ [float("inf")] * len(path_array[q]) for q in range(Q - 1)]
    dp[0][0] = 0
    last_passed = [-1] * N
    last_passed[path_array[0][0]] = (0, 0)
    for q in range(Q - 1):
        for i in range(1, len(path_array[q])):
            dp[q][i] = dp[q][i - 1] + edges[(path_array[q][i - 1], path_array[q][i])]
            if last_passed[path_array[q][i]] != -1:
                q_, i0 = last_passed[path_array[q][i]]
                d = cum_dist_array[q][0] - cum_dist_array[q_][i0]
                dp[q][i] = min(dp[q][i], dp[q_][i0] + d + C)
            last_passed[path_array[q][i]] = (q, i)
        
        if q < Q - 2:
            dp[q + 1][0] = dp[q][-1]
            last_passed[path_array[q + 1][0]] = (q + 1, 0)
    print(dp[-1][-1])














    



if __name__ == "__main__":
    main()
0