-
Notifications
You must be signed in to change notification settings - Fork 0
/
743-NetworkDelayTime.py
34 lines (30 loc) · 1.2 KB
/
743-NetworkDelayTime.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
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
cost = [101] * N
cost[K-1] = 0
visited = set()
not_visited = set(range(1, N+1))
while len(list(visited)) != N:
# Select the current node
remaining = list(not_visited.difference(visited))
remaining = [(vertex, cost[vertex-1]) for vertex in remaining]
remaining.sort(key=lambda tup: tup[1])
selected_vertex = remaining[0][0]
# Update cost through the current node
relevant_time = [
time for time in times if time[0] == selected_vertex]
# print(relevant_time)
for time in relevant_time:
if cost[time[1]-1] > cost[time[0]-1] + time[2]:
cost[time[1]-1] = cost[time[0]-1] + time[2]
# print(selected_vertex)
# Remove current node from not_visited and add to visited
visited = visited.union(set([selected_vertex]))
not_visited.remove(selected_vertex)
return -1 if 101 in cost else max(cost)