結果

問題 No.1308 ジャンプビーコン
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-01-07 21:34:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,006 ms / 4,000 ms
コード長 2,295 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 82,196 KB
実行使用メモリ 469,380 KB
最終ジャッジ日時 2024-09-27 19:32:37
合計ジャッジ時間 19,707 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,592 KB
testcase_01 AC 37 ms
55,492 KB
testcase_02 AC 36 ms
55,524 KB
testcase_03 AC 42 ms
61,900 KB
testcase_04 AC 43 ms
60,564 KB
testcase_05 AC 40 ms
60,848 KB
testcase_06 AC 43 ms
60,828 KB
testcase_07 AC 41 ms
60,964 KB
testcase_08 AC 74 ms
77,212 KB
testcase_09 AC 67 ms
74,248 KB
testcase_10 AC 71 ms
74,488 KB
testcase_11 AC 63 ms
72,568 KB
testcase_12 AC 65 ms
75,024 KB
testcase_13 AC 115 ms
79,084 KB
testcase_14 AC 105 ms
79,288 KB
testcase_15 AC 107 ms
78,832 KB
testcase_16 AC 113 ms
78,504 KB
testcase_17 AC 113 ms
77,628 KB
testcase_18 AC 623 ms
83,828 KB
testcase_19 AC 605 ms
83,912 KB
testcase_20 AC 633 ms
82,828 KB
testcase_21 AC 537 ms
80,860 KB
testcase_22 AC 522 ms
81,256 KB
testcase_23 AC 37 ms
54,300 KB
testcase_24 AC 74 ms
78,704 KB
testcase_25 AC 69 ms
78,644 KB
testcase_26 AC 504 ms
82,036 KB
testcase_27 AC 511 ms
82,000 KB
testcase_28 AC 528 ms
82,376 KB
testcase_29 AC 1,274 ms
224,876 KB
testcase_30 AC 1,348 ms
224,244 KB
testcase_31 AC 3,006 ms
469,380 KB
testcase_32 AC 1,707 ms
227,844 KB
testcase_33 AC 1,470 ms
224,436 KB
testcase_34 AC 538 ms
81,424 KB
testcase_35 AC 556 ms
81,776 KB
testcase_36 AC 422 ms
82,736 KB
testcase_37 AC 409 ms
81,632 KB
testcase_38 AC 803 ms
86,900 KB
testcase_39 AC 711 ms
86,376 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