結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー nephrologistnephrologist
提出日時 2021-03-26 22:32:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,287 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 82,100 KB
実行使用メモリ 99,380 KB
最終ジャッジ日時 2024-05-06 16:18:30
合計ジャッジ時間 8,174 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
57,984 KB
testcase_01 AC 39 ms
52,480 KB
testcase_02 AC 2,682 ms
97,368 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline

infi = 1 << 60

from heapq import heappop, heappush

n, k = map(int, input().split())

graph = [[] for _ in range(n + 2 * k)]
for _ in range(n - 1):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append((b, c))
    graph[b].append((a, c))


for i in range(k):
    m, p = map(int, input().split())
    X = list(map(int, input().split()))
    vin = n + 2 * i
    vout = n + 2 * i + 1
    for x in X:
        x -= 1
        graph[x].append((vin, 0))
        graph[vout].append((x, 0))
    graph[vin].append((vout, p))


def dijkstra(start, graph, goal):
    infi = 1 << 60
    mask = 1 << 18
    pq = []
    dist = [infi] * (n + 2 * k)
    # dist[start]=0
    heappush(pq, 0 * mask + start)
    while pq:
        temp = heappop(pq)
        d, v = temp // mask, temp & (mask - 1)
        if dist[v] < d:
            continue
        dist[v] = d
        if v == goal:
            return dist[v]
        for u, delta in graph[v]:
            nd = d + delta
            # print("u", u, "nd", nd)
            if dist[u] > nd:
                heappush(pq, nd * mask + u)
    return dist


q = int(input())
for _ in range(q):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    print(dijkstra(u, graph, v))
0