-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaeromocas.py
134 lines (92 loc) · 3.12 KB
/
aeromocas.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.first = None
self.last = None
self.size = 0
def inqueue(self, data):
node = Node(data) # create new node
if self.isEmpty():
self.first = node
self.last = node
self.size += 1
else:
self.last.next = node
self.last = node
self.size += 1
def dequeue(self):
if self.isEmpty():
print('Queue is empty!')
return None
else:
first_data = self.first.data
if self.first is self.last:
self.first = None
self.last = None
self.size -= 1
else:
self.first = self.first.next
self.size -= 1
return first_data
def isEmpty(self):
return self.size == 0
# Returns the maximum value of a list
def maximum(list):
max_value = list[0]
for i in list:
if i > max_value:
max_value = i
return max_value
def dijkstra(graph, orig_vert, len_dist):
# The index is the vertice and the values are the distances.
# distance = [0, inf, inf, ... , inf] if orig_vert is equal to first vertice.
distance = [float('inf')] * len_dist
distance[orig_vert-1] = 0
q = Queue()
# Queue a tuple, (vertice, distance of that vertice).
q.inqueue((orig_vert, distance[orig_vert-1]))
while not q.isEmpty():
# deQueue a tuple = (vertice, distance)
t = q.dequeue()
vertice = t[0]
min_vertice_distance = t[1]
if min_vertice_distance == distance[vertice-1]:
# v is every adjacent of vertice.
for v in graph[vertice-1]:
vert = v[0]
vert_dist = v[1]
if distance[vertice-1] + vert_dist < distance[vert-1]:
distance[vert-1] = distance[vertice-1] + vert_dist
q.inqueue((vert, distance[vert-1]))
#Return all distances from orig_vert vertice to the last vertice
return maximum(distance)
# returns the length of a list
def len_list(list):
len = int(0)
for i in list: len += 1
return len
#Returns the minimum value of a list
def minimum(list):
min_value = list[0]
for i in list:
if i < min_value:
min_value = i
return min_value
def main():
N, M = [int(n) for n in input().split()]
grafo = [[] for list in range(1, N+1)]
all_distances = [[] for list in range(1, N+1)]
# Creates a graph = [[adjs of vertice 1], [adjs of vertice 2], ... , [adjs of vertice n]]
# The vertice ("name") is (index + 1)
for i in range(M):
U, V, W = [int(n) for n in input().split()]
grafo[U] += [(V+1,W)]
grafo[V] += [(U+1,W)]
len_dist = len_list(grafo)
for v in range(0, len_dist):
all_distances[v] = dijkstra(grafo, v+1, len_dist)
print(minimum(all_distances))
main()