-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemB.py
55 lines (41 loc) · 1.13 KB
/
ProblemB.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
from collections import defaultdict
from itertools import combinations
def minGoldBFS(graph, s, t):
if graph[t] == [] and len(graph) > 1 and s != t:
return 'Impossible'
distances = [None] * (len(graph))
distances[s] = 0
queue = []
queue.append(s)
while queue:
u = queue.pop(0)
if u == t:
return distances[u]
for v in graph[u]:
if distances[v] == None:
distances[v] = distances[u] + 1
queue.append(v)
return 'Impossible'
def main():
str_n, str_m, str_k, str_s, str_t = input().split()
n = int(str_n)
m = int(str_m)
k = int(str_k)
s = int(str_s)
t = int(str_t)
g = []
for i in range(n+1):
g.append([])
a = input().split()
for i in range(k):
g[n].append(int(a[i]) - 1)
g[int(a[i]) - 1].append(n)
for i in range(m):
v1, v2 = input().split()
u = int(v1) - 1
v = int(v2) - 1
g[u].append(v)
g[v].append(u)
print(minGoldBFS(g, s - 1, t - 1), '\n')
if __name__ == "__main__":
main()