-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9694.py
37 lines (33 loc) · 951 Bytes
/
9694.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys
from heapq import *
def dijkstra(start):
q = []
distance[start] = 0
who_met[start] = [start]
heappush(q, (0, start))
while q:
dist, u = heappop(q)
if distance[u] < dist:
continue
for v, w in graph[u]:
if distance[v] <= dist + w:
continue
distance[v] = dist + w
who_met[v] = who_met[u] + [v]
heappush(q, (distance[v], v))
INF = float('inf')
T = int(sys.stdin.readline())
for t in range(1, T + 1):
n, m = map(int, sys.stdin.readline().split())
graph = [[] for _ in range(m)]
distance = [INF] * m
who_met = [[] for _ in range(m)]
for _ in range(n):
u, v, w = map(int, sys.stdin.readline().split())
graph[u].append([v, w])
graph[v].append([u, w])
dijkstra(0)
if distance[-1] == INF:
print(f'Case #{t}: -1')
else:
print(f'Case #{t}:', *who_met[-1])