- 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
- 80 lines of Python from the credited upstream file abc237_e.py.
- The implementation visibly relies on sequence storage, ordered lookup, 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.
12 3 4def dijkstra(vertex_count: int, source: int, edges):5 """Uses Dijkstra's algorithm to find the shortest path in a graph.6 Args:7 vertex_count: The number of vertices.8 source : Vertex number (0-indexed).9 edges : List of (cost, edge) (0-indexed).10 Returns:11 costs : List of the shortest distance.12 parents: List of parent vertices.13 Landau notation: O(|Edges|log|Vertices|).14 See:15 https:atcoder.jp/contests/abc191/submissions/1996407816 https:atcoder.jp/contests/abc191/submissions/1996623217 """18 19 from heapq import heappop, heappush20 21 hq = [(0, source)] 22 costs = [float("inf") for _ in range(vertex_count)]23 costs[source] = 024 visited = [False for _ in range(vertex_count)]25 pending = -126 parents = [pending for _ in range(vertex_count)]27 28 while hq:29 cost, vertex = heappop(hq)30 31 if cost > costs[vertex]:32 continue33 34 if visited[vertex]:35 continue36 37 visited[vertex] = True38 39 for weight, edge in edges[vertex]:40 new_cost = cost + weight41 42 if new_cost < costs[edge]:43 costs[edge] = new_cost44 parents[edge] = vertex45 heappush(hq, (new_cost, edge))46 47 return costs, parents48 49 50def main():51 import sys52 53 input = sys.stdin.readline54 55 n, m = map(int, input().split())56 h = list(map(int, input().split()))57 edges = [[] for _ in range(n)]58 59 for _ in range(m):60 ai, bi = map(int, input().split())61 ai -= 162 bi -= 163 64 65 66 edges[ai].append((max(0, h[bi] - h[ai]), bi))67 edges[bi].append((max(0, h[ai] - h[bi]), ai))68 69 dist, _ = dijkstra(vertex_count=n, source=0, edges=edges)70 ans = 071 72 for i in range(n):73 ans = max(ans, h[0] - h[i] - dist[i])74 75 print(ans)76 77 78if __name__ == "__main__":79 main()80