| 28 | return ns |
| 29 | |
| 30 | def dijkstra(cave: list[list[int]], start: Node, target: Node) -> int: |
| 31 | q, costs = [(0,start)], defaultdict[Node,int](lambda: 100000) |
| 32 | costs[start] = 0 |
| 33 | while q: |
| 34 | cost,here = heappop(q) |
| 35 | if here == target: |
| 36 | return cost |
| 37 | for c,node in neighbors(cave,here): |
| 38 | c += cost |
| 39 | if c < costs[node]: |
| 40 | costs[node] = c |
| 41 | heappush(q, (c,node)) |
| 42 | assert False |
| 43 | |
| 44 | def build_cave(depth: int, rows: int, cols: int) -> tuple[int, list[list[int]]]: |
| 45 | cave = [[0 for _ in range(10*cols)] for _ in range(10*rows)] |