- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 53 lines of Python from the credited upstream file 1928.py.
- The implementation visibly relies on sequence storage, work queue.
- No explicit loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def minCost(3 self,4 maxTime: int,5 edges: list[list[int]],6 passingFees: list[int],7 ) -> int:8 n = len(passingFees)9 graph = [[] for _ in range(n)]10 11 for u, v, w in edges:12 graph[u].append((v, w))13 graph[v].append((u, w))14 15 return self._dijkstra(graph, 0, n - 1, maxTime, passingFees)16 17 def _dijkstra(18 self,19 graph: list[list[tuple[int, int]]],20 src: int,21 dst: int,22 maxTime: int,23 passingFees: list[int],24 ) -> int:25 26 cost = [math.inf] * len(graph)27 28 dist = [maxTime + 1] * len(graph)29 30 cost[src] = passingFees[src]31 dist[src] = 032 minHeap = [(cost[src], dist[src], src)] 33 34 while minHeap:35 currCost, d, u = heapq.heappop(minHeap)36 if u == dst:37 return cost[dst]38 if d > dist[u] and currCost > cost[u]:39 continue40 for v, w in graph[u]:41 if d + w > maxTime:42 continue43 44 if currCost + passingFees[v] < cost[v]:45 cost[v] = currCost + passingFees[v]46 dist[v] = d + w47 heapq.heappush(minHeap, (cost[v], dist[v], v))48 elif d + w < dist[v]:49 dist[v] = d + w50 heapq.heappush(minHeap, (currCost + passingFees[v], dist[v], v))51 52 return -153