import sys, heapq
input=sys.stdin.readline
n = int(input())
maps = []
#배열 입력
for i in range(n):
tmp = list(map(int,input().split()))
maps.append(tmp)
#비용 초기화
INF = int(1e9)
cost = [[INF]*n for _ in range(n)]
def dijkstra():
q=[]
#담을 리스트 q와 위치까지 드는 최소 비용, 그리고 위치
heapq.heappush(q,(0,(0,0)))
moves = [(1,0),(0,1)]
cost[0][0] = 0
while q:
# 가장 최단 거리가 짧은 노드에 대한 정보 꺼내기
dist, now = heapq.heappop(q)
#현재 노드가 이미 처리된 적이 있는 노드라면 무시
if cost[now[0]][now[1]] < dist:
continue
#현재 노드와 연결된 다른 입접한 노드들을 확인
for move in moves:
row = now[0]+move[0]
col = now[1]+move[1]
#이동할 수 없는 경우
if row ==n or col==n:
continue
button = 0
#제약 조건 확인 후 계산
if maps[row][col]>=maps[now[0]][now[1]]:
button+=maps[row][col]-maps[now[0]][now[1]]+1
hap=dist+button
#지금 노드를 거쳐서 가는게 더 효율적인 경우
if hap < cost[row][col]:
cost[row][col] = hap
heapq.heappush(q,(hap,(row,col)))
dijkstra()
# for i in range(n):
# for j in range(n):
# print(cost[i][j],end=" ")
# print('')
print(cost[n-1][n-1])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2924 KB |
Output is correct |
2 |
Correct |
17 ms |
2924 KB |
Output is correct |
3 |
Correct |
16 ms |
2924 KB |
Output is correct |
4 |
Correct |
17 ms |
2924 KB |
Output is correct |
5 |
Correct |
16 ms |
2924 KB |
Output is correct |
6 |
Correct |
18 ms |
2924 KB |
Output is correct |
7 |
Correct |
22 ms |
2924 KB |
Output is correct |
8 |
Correct |
17 ms |
2924 KB |
Output is correct |
9 |
Correct |
21 ms |
2924 KB |
Output is correct |
10 |
Correct |
17 ms |
2924 KB |
Output is correct |
11 |
Correct |
18 ms |
3052 KB |
Output is correct |
12 |
Correct |
17 ms |
2924 KB |
Output is correct |
13 |
Correct |
17 ms |
2924 KB |
Output is correct |
14 |
Correct |
17 ms |
2924 KB |
Output is correct |
15 |
Correct |
17 ms |
2924 KB |
Output is correct |
16 |
Correct |
18 ms |
2924 KB |
Output is correct |
17 |
Correct |
17 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2924 KB |
Output is correct |
2 |
Correct |
19 ms |
2924 KB |
Output is correct |
3 |
Correct |
19 ms |
3052 KB |
Output is correct |
4 |
Correct |
18 ms |
2924 KB |
Output is correct |
5 |
Correct |
18 ms |
2924 KB |
Output is correct |
6 |
Correct |
21 ms |
2924 KB |
Output is correct |
7 |
Correct |
18 ms |
2924 KB |
Output is correct |
8 |
Correct |
17 ms |
2924 KB |
Output is correct |
9 |
Correct |
19 ms |
2924 KB |
Output is correct |
10 |
Correct |
23 ms |
3052 KB |
Output is correct |
11 |
Correct |
17 ms |
3072 KB |
Output is correct |
12 |
Correct |
17 ms |
2924 KB |
Output is correct |
13 |
Correct |
19 ms |
2944 KB |
Output is correct |
14 |
Correct |
17 ms |
2924 KB |
Output is correct |
15 |
Correct |
18 ms |
2924 KB |
Output is correct |
16 |
Correct |
17 ms |
2924 KB |
Output is correct |
17 |
Correct |
17 ms |
2924 KB |
Output is correct |
18 |
Correct |
20 ms |
2924 KB |
Output is correct |
19 |
Correct |
18 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
5484 KB |
Output is correct |
2 |
Correct |
259 ms |
5724 KB |
Output is correct |
3 |
Correct |
293 ms |
5612 KB |
Output is correct |
4 |
Correct |
253 ms |
5740 KB |
Output is correct |
5 |
Correct |
246 ms |
5612 KB |
Output is correct |
6 |
Correct |
255 ms |
5776 KB |
Output is correct |
7 |
Correct |
256 ms |
5660 KB |
Output is correct |
8 |
Correct |
230 ms |
5612 KB |
Output is correct |
9 |
Correct |
243 ms |
5612 KB |
Output is correct |
10 |
Correct |
229 ms |
5476 KB |
Output is correct |
11 |
Correct |
230 ms |
5484 KB |
Output is correct |
12 |
Correct |
230 ms |
5484 KB |
Output is correct |
13 |
Correct |
258 ms |
5612 KB |
Output is correct |
14 |
Correct |
212 ms |
5484 KB |
Output is correct |
15 |
Correct |
205 ms |
4844 KB |
Output is correct |
16 |
Correct |
241 ms |
5612 KB |
Output is correct |
17 |
Correct |
229 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1139 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |