結果

問題 No.1283 Extra Fee
ユーザー roarisroaris
提出日時 2020-11-14 02:55:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,512 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 101,248 KB
最終ジャッジ日時 2024-11-16 07:12:32
合計ジャッジ時間 16,508 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,712 KB
testcase_01 AC 37 ms
52,708 KB
testcase_02 AC 38 ms
53,252 KB
testcase_03 AC 35 ms
53,192 KB
testcase_04 AC 36 ms
53,216 KB
testcase_05 AC 37 ms
53,180 KB
testcase_06 AC 38 ms
53,552 KB
testcase_07 AC 37 ms
54,120 KB
testcase_08 AC 39 ms
54,336 KB
testcase_09 AC 37 ms
52,880 KB
testcase_10 AC 41 ms
53,192 KB
testcase_11 AC 167 ms
78,132 KB
testcase_12 AC 199 ms
78,032 KB
testcase_13 AC 132 ms
77,076 KB
testcase_14 AC 262 ms
78,832 KB
testcase_15 AC 324 ms
79,656 KB
testcase_16 AC 147 ms
77,512 KB
testcase_17 AC 1,443 ms
100,604 KB
testcase_18 AC 979 ms
90,928 KB
testcase_19 AC 952 ms
89,204 KB
testcase_20 AC 913 ms
89,092 KB
testcase_21 AC 930 ms
89,072 KB
testcase_22 AC 829 ms
87,140 KB
testcase_23 AC 853 ms
84,924 KB
testcase_24 AC 910 ms
84,972 KB
testcase_25 AC 1,040 ms
90,644 KB
testcase_26 AC 1,034 ms
90,400 KB
testcase_27 AC 991 ms
89,668 KB
testcase_28 AC 1,026 ms
90,424 KB
testcase_29 AC 1,512 ms
101,248 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