-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_4485.py
45 lines (34 loc) · 896 Bytes
/
BOJ_4485.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
import heapq
input = sys.stdin.readline
INF = sys.maxsize
dire = [[1, 0],
[0, 1],
[-1, 0],
[0, -1]]
def dijk(n):
dist = [[INF for _ in range(n)] for _ in range(n)]
q = []
dist[0][0] = graph[0][0]
heapq.heappush(q, [dist[0][0], 0, 0])
while q:
now_dist, y, x = heapq.heappop(q)
for ty, tx in dire:
ny = y + ty
nx = x + tx
if 0 <= ny < n and 0 <= nx < n:
ndist = now_dist + graph[ny][nx]
if dist[ny][nx] > ndist:
dist[ny][nx] = ndist
heapq.heappush(q, [ndist, ny, nx])
return dist[n-1][n-1]
i = 1
while True:
n = int(input())
if n == 0:
break
graph = []
for _ in range(n):
graph.append(list(map(int, input().split())))
print("Problem %d: %d" %(i, dijk(n)))
i += 1