-
Notifications
You must be signed in to change notification settings - Fork 0
/
j2_03.py
41 lines (37 loc) · 1.13 KB
/
j2_03.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
def solve(N, M, edge_list, start, end):
# Initalization.
dist = 1234567890
curr_dist = 0
visited = [ False for i in range(N) ]
vertex_stack = [] # note: an empty list, being used as an empty stack
index_stack = []
vertex_stack.append( (start, 0) )
index_stack.append(0)
visited[start] = True
while len(vertex_stack) > 0:
u, d = vertex_stack[-1]
k = index_stack.pop()
if u == end:
dist = min(dist, curr_dist)
while k < M:
p, q, w = edge_list[k]
# Find an edge starting at u and save the other endpoint in v.
v = -1
if p == u: v = q
elif q == u: v = p
if v >= 0 and not visited[v]:
break
else:
k += 1
if k == M:
visited[u] = False
vertex_stack.pop()
if len(vertex_stack) > 0:
curr_dist -= d
else:
index_stack.append(k + 1)
vertex_stack.append((v, w))
index_stack.append(0)
visited[v] = True
curr_dist += w
return dist