-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path73.py
More file actions
49 lines (38 loc) · 2.27 KB
/
73.py
File metadata and controls
49 lines (38 loc) · 2.27 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
#https://www.spoj.com/problems/TRVCOST/
'''
Comment
'''
import queue
INF = 60000
grp = [[] for _ in range(510)]
dist = [INF for _ in range(510)]
class Node:
def __init__(seft, _vertex, _weight):
seft.vertex = _vertex
seft.weight = _weight
def __lt__(seft, other):
return seft.weight < other.weight
def dijkstra(src):
pq = queue.PriorityQueue()
pq.put(Node(src, 0))
dist[src] = 0
while not pq.empty():
top = pq.get()
v = top.vertex
w = top.weight
for neightbor in grp[v]:
if w + neightbor.weight < dist[neightbor.vertex]:
dist[neightbor.vertex] = w + neightbor.weight
pq.put(Node(neightbor.vertex, dist[neightbor.vertex]))
if __name__=="__main__":
n = int(input())
for _ in range(n):
a, b, w = map(int, input().split())
grp[a].append(Node(b, w))
grp[b].append(Node(a, w))
src = int(input())
qry = int(input())
dijkstra(src)
for _ in range(qry):
des = int(input())
print(dist[des] if dist[des] != INF else "NO PATH")