-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 15 (Shortest Reach in a Graph).py3
49 lines (46 loc) · 1.38 KB
/
Day 15 (Shortest Reach in a Graph).py3
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
class Graph():
def __init__(self, n):
self.nodes={}
for i in range(n):
self.nodes[i]=set()
def connect(self, x, y):
self.nodes[x].add(y)
self.nodes[y].add(x)
def find_all_distances(self, s):
edge_distance=6
curr_nodes=[s]
next_nodes=[]
shortest_distances={}
curr_length=0
while True:
if curr_nodes==[] and next_nodes==[]:
break
if curr_nodes==[]:
curr_nodes=next_nodes
curr_length+=edge_distance
next_nodes=[]
this_node=curr_nodes.pop()
if this_node in shortest_distances:
continue
else:
shortest_distances[this_node]=curr_length
for x in self.nodes[this_node]:
next_nodes.append(x)
result=""
for i in range(len(self.nodes)):
if i==s:
continue
if i in shortest_distances:
result+=str(shortest_distances[i])+' '
else:
result+='-1 '
print(result)
t = int(input())
for i in range(t):
n,m = [int(value) for value in input().split()]
graph = Graph(n)
for i in range(m):
x,y = [int(x) for x in input().split()]
graph.connect(x-1,y-1)
s = int(input())
graph.find_all_distances(s-1)