-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.py~
72 lines (60 loc) · 1.7 KB
/
reverse.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
71
72
import heapq
"""
http://www.codechef.com/AUG14/problems/REVERSE/
"""
def dijkstra(graph,start, goal):
sptset=[False]*goal
distance=[10**6 for x in xrange(goal)]
distance[start-1]=0
heap=[]
u=start-1
heapq.heappush(heap,(0,u))
while heap:
minidist, u = heapq.heappop(heap)
distance[u]=minidist
#print "u value%d"%u
#print "distance of u%d"%distance[u]
if u==goal-1:
#print "in break"
break
else:
#print "in else"
sptset[u]=True
for next in graph[u]:
#print "next value is %d"%next
#print not sptset[next]
#print distance[u]+matrix[u][next]
#print distance[next]
if not sptset[next] and distance[u]+graph[u][next] < distance[next]:
distance[next]=distance[u]+graph[u][next]
heapq.heappush(heap, (distance[next], next))
#print "distance[next]%d"%distance[next]
#print sptset
if distance[goal-1]==10**6:
print -1
else:
print distance[goal-1]
n,m=map(int,raw_input().split())
graph={}
for j in xrange(m):
p,q=map(int,raw_input().split())
x=p-1
y=q-1
#constructing undirected graph(adjacency list representation)
if x in graph:
if y in graph[x]:
if graph[x][y]==1:
graph[x][y]=0
else:
graph[x][y]=0
else:
graph[x]={}
graph[x][y]=0
if y in graph:
if x not in graph[y]:
graph[y][x]=1
else:
graph[y]={}
graph[y][x]=1
#print graph
dijkstra(graph,1, n)