우선순위 큐는 다익스트라 알고리즘의 병목현상을 개선함.
import sys
from math import inf
from heapq import heappush, heappop
input = sys.stdin.readline
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def dijkstra():
q = []
dist[0][0] = graph[0][0] # 시작노드의 비용 설정
heappush(q, (dist[0][0], [0, 0])) # 시작 노드부터의 x 노드까지의 비용, x의 좌표
while q:
d, node = heappop(q)
x, y = node
if dist[x][y] < d: # 큐에서 뽑아낸 거리가 이미 갱신된 거리보다 큰 경우 이미 방문한 셈이므로 무시
continue
for i, j in zip(dx, dy):
n_x, n_y = x + i, y + j
if 0 <= n_x < N and 0 <= n_y < N: # in range check
cost = d + graph[n_x][n_y] # 시작->현재 노드까지의 비용 + 현재노드->다음노드까지의 비용
if cost < dist[n_x][n_y]: # 시작->다음노드까지의 비용보다 적을 경우
dist[n_x][n_y] = cost
heappush(q, (cost, [n_x, n_y]))
return dist[N - 1][N - 1]
if __name__ == '__main__':
caseNum = 0
while True:
caseNum += 1
N = int(input())
if N == 0:
exit(0)
graph = [list(map(int, input().split())) for _ in range(N)]
dist = [[inf for _ in range(N)] for _ in range(N)]
print("Problem {}: {}".format(caseNum, dijkstra()))