-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafePathFinder.py
73 lines (61 loc) · 1.69 KB
/
SafePathFinder.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# import sys
import math
import heapq
class Node:
def __init__(self, v,u, w):
self.v = v
self.u = u
self.w = w
def getV(self):
return self.v
def getU(self):
return self.u
def getWeight(self):
return self.w
def dijkstra(n, graph, source):
distances = [float('inf')] * n
distances[source] = 0
q = []
heapq.heappush(q, (0, source))
while q:
current = heapq.heappop(q)
for node in graph[current[1]]:
if distances[current[1]] + node.w < distances[node.v]:
distances[node.v] = distances[current[1]] + node.w
heapq.heappush(q, (distances[node.v], node.v))
return distances
# def dijkstra(n, graph, source):
# distances = [math.inf] * n
# distances[source] = 0
# visited = [False] * n
# for _ in range(n):
# u = -1
# for i in range(n):
# if not visited[i] and (u == -1 or distances[i] < distances[u]):
# u = i
# visited[u] = True
# for node in graph[u]:
# v = node.v
# w = node.w
# if distances[u] + w < distances[v]:
# distances[v] = distances[u] + w
# return distances
n, m,s, t,k = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(m):
u, v, w = map(int, input().split())
adj[u-1].append(Node(v-1,u-1, w))
adj[v-1].append(Node(u-1,v-1, w))
guards = []
guards=map(int, input().split())
# print(guards)
dist = dijkstra(n, adj, t-1)
canBeCaught = False
for guard in guards:
if dist[guard-1] <= dist[s-1]:
canBeCaught = True
break
if canBeCaught:
print("impossible")
else:
print(dist[s-1])