forked from minaevd/hackerrank-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_shortest_search1.py
More file actions
executable file
·59 lines (46 loc) · 1.25 KB
/
Copy pathgraph_shortest_search1.py
File metadata and controls
executable file
·59 lines (46 loc) · 1.25 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
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
T = int(raw_input().strip())
for t in xrange(T):
N,M = map(int, raw_input().strip().split())
nodes = dict()
for i in xrange(N):
nodes[i] = []
for i in xrange(M):
x,y = map(int, raw_input().strip().split())
nodes[x-1].extend([y-1])
S = int(raw_input().strip())-1
# 1
# 5 5
# 1 2
# 1 3
# 2 4
# 3 5
# 4 5
# 1
# print nodes
# print find_shortest_path(nodes, S, 4)
# exit()
for i in xrange(N):
if S == i: continue
#print "DEBUG: search for path between ",str(S),"and",str(i)
#print graph.path(S, i)
path = find_shortest_path(nodes, S, i)
if path == None:
print -1,
else:
print (len(path)-1)*6,
print