- 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
- 55 lines of Python from the credited upstream file 2699.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 modifiedGraphEdges(self, n: int, edges: list[list[int]], source: int, destination: int, target: int) -> list[list[int]]:3 MAX = 2_000_000_0004 graph = [[] for _ in range(n)]5 6 for u, v, w in edges:7 if w == -1:8 continue9 graph[u].append((v, w))10 graph[v].append((u, w))11 12 distToDestination = self._dijkstra(graph, source, destination)13 if distToDestination < target:14 return []15 if distToDestination == target:16 17 for edge in edges:18 if edge[2] == -1:19 edge[2] = MAX20 return edges21 22 for i, (u, v, w) in enumerate(edges):23 if w != -1:24 continue25 edges[i][2] = 126 graph[u].append((v, 1))27 graph[v].append((u, 1))28 distToDestination = self._dijkstra(graph, source, destination)29 if distToDestination <= target:30 edges[i][2] += target - distToDestination31 32 for j in range(i + 1, len(edges)):33 if edges[j][2] == -1:34 edges[j][2] = MAX35 return edges36 37 return []38 39 def _dijkstra(self, graph: list[list[int]], src: int, dst: int) -> int:40 dist = [math.inf] * len(graph)41 42 dist[src] = 043 minHeap = [(dist[src], src)] 44 45 while minHeap:46 d, u = heapq.heappop(minHeap)47 if d > dist[u]:48 continue49 for v, w in graph[u]:50 if d + w < dist[v]:51 dist[v] = d + w52 heapq.heappush(minHeap, (dist[v], v))53 54 return dist[dst]55