結果

問題 No.1283 Extra Fee
ユーザー roarisroaris
提出日時 2020-11-14 02:55:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,735 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 100,980 KB
最終ジャッジ日時 2024-04-27 23:41:33
合計ジャッジ時間 18,899 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,820 KB
testcase_01 AC 39 ms
53,356 KB
testcase_02 AC 40 ms
53,572 KB
testcase_03 AC 40 ms
52,740 KB
testcase_04 AC 40 ms
52,980 KB
testcase_05 AC 41 ms
53,856 KB
testcase_06 AC 42 ms
53,540 KB
testcase_07 AC 41 ms
53,312 KB
testcase_08 AC 44 ms
53,756 KB
testcase_09 AC 42 ms
53,456 KB
testcase_10 AC 41 ms
53,248 KB
testcase_11 AC 186 ms
77,872 KB
testcase_12 AC 216 ms
78,292 KB
testcase_13 AC 149 ms
77,308 KB
testcase_14 AC 287 ms
78,732 KB
testcase_15 AC 364 ms
79,908 KB
testcase_16 AC 161 ms
77,632 KB
testcase_17 AC 1,614 ms
100,608 KB
testcase_18 AC 1,117 ms
91,184 KB
testcase_19 AC 1,079 ms
89,148 KB
testcase_20 AC 1,048 ms
89,112 KB
testcase_21 AC 1,058 ms
89,072 KB
testcase_22 AC 949 ms
86,892 KB
testcase_23 AC 965 ms
85,208 KB
testcase_24 AC 979 ms
84,840 KB
testcase_25 AC 1,152 ms
90,272 KB
testcase_26 AC 1,176 ms
90,176 KB
testcase_27 AC 1,134 ms
89,544 KB
testcase_28 AC 1,160 ms
90,044 KB
testcase_29 AC 1,735 ms
100,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import *

def dijkstra():
    dist = [[[10**18]*N for _ in range(N)] for _ in range(2)]
    dist[0][0][0] = 0
    pq = []
    heappush(pq, (0, 0, 0, 0))
    
    while pq:
        d, f, x, y = heappop(pq)
        
        if dist[f][x][y]<d:
            continue
        
        for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
            if 0<=nx<N and 0<=ny<N:
                if dist[f][nx][ny]>dist[f][x][y]+cost[nx][ny]:
                    dist[f][nx][ny] = dist[f][x][y]+cost[nx][ny]
                    heappush(pq, (dist[f][nx][ny], f, nx, ny))
                
                if f==0 and cost[nx][ny]>1 and dist[1][nx][ny]>dist[0][x][y]+1:
                    dist[1][nx][ny] = dist[0][x][y]+1
                    heappush(pq, (dist[1][nx][ny], 1, nx, ny))
    
    return min(dist[0][-1][-1], dist[1][-1][-1])

N, M = map(int, input().split())
cost = [[1]*N for _ in range(N)]

for _ in range(M):
    h, w, c = map(int, input().split())
    cost[h-1][w-1] += c

print(dijkstra())
0