-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-time-to-visit-disappearing-nodes.py
37 lines (33 loc) · 1.19 KB
/
minimum-time-to-visit-disappearing-nodes.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
# Time: O((|E| + |V|) * log|V|) = O(|E| * log|V|) by using binary heap,
# if we can further to use Fibonacci heap, it would be O(|E| + |V| * log|V|)
# Space: O(|E| + |V|) = O(|E|)
import heapq
# dijkstra's algorithm
class Solution(object):
def minimumTime(self, n, edges, disappear):
"""
:type n: int
:type edges: List[List[int]]
:type disappear: List[int]
:rtype: List[int]
"""
INF = float("inf")
def modified_dijkstra(start):
best = [-1]*n
best[start] = 0
min_heap = [(best[start], start)]
while min_heap:
curr, u = heapq.heappop(min_heap)
if curr != best[u]:
continue
for v, w in adj[u]:
if not curr+w < min(best[v] if best[v] != -1 else INF, disappear[v]): # modified
continue
best[v] = curr+w
heapq.heappush(min_heap, (best[v], v))
return best
adj = [[] for _ in xrange(n)]
for u, v, w in edges:
adj[u].append((v, w))
adj[v].append((u, w))
return modified_dijkstra(0)