-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest path in a graph.py
60 lines (56 loc) · 1.38 KB
/
shortest path in a graph.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
def find(graph, var):
for i in graph:
if var in graph[i] and i == 1:
return True
else:
if var in graph[i]:
var = i
return find(graph, var)
return False
def weightchange(graph, weights):
weight_ref = [0]*n
#print(weight_ref)
#print(graph)
#print(weights)
for i in weights.items():
for j in graph[i[0][0]]:
for k in weights:
if i[0] == k and i[0][-1] == j:
if weight_ref[j-1] == 0:
weight_ref[j-1] = i[1][0]
else:
#print(i)
#print(j)
#print("weight",weight_ref[j-1])
#print("weight - i ", weight_ref[i[0][0]-1])
total = i[1][0] + weight_ref[i[0][0]-1]
if total < weight_ref[j-1]:
weight_ref[j-1] = total
return(weight_ref)
n, m = map(int,input().split())
graph = {}
weights = {}
for i in range(m):
u, v, w = map(int,input().split())
if u not in graph:
graph[u] = [v]
else:
tem = graph[u]
tem.append(v)
graph[u] = tem
weights[(u,v)] = [w]
var = n
if find(graph, var):
x = weightchange(graph, weights)
print(x[-1])
else:
print("-1")
'''
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
'''