結果

問題 No.1283 Extra Fee
ユーザー DrDrpilotDrDrpilot
提出日時 2022-03-15 13:10:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,422 ms / 2,000 ms
コード長 1,090 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 120,928 KB
最終ジャッジ日時 2024-04-28 00:49:58
合計ジャッジ時間 18,814 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,480 KB
testcase_01 AC 41 ms
52,992 KB
testcase_02 AC 39 ms
53,376 KB
testcase_03 AC 37 ms
52,736 KB
testcase_04 AC 36 ms
52,736 KB
testcase_05 AC 35 ms
52,608 KB
testcase_06 AC 39 ms
53,120 KB
testcase_07 AC 37 ms
52,992 KB
testcase_08 AC 39 ms
53,632 KB
testcase_09 AC 39 ms
53,504 KB
testcase_10 AC 37 ms
53,248 KB
testcase_11 AC 193 ms
79,724 KB
testcase_12 AC 215 ms
80,004 KB
testcase_13 AC 152 ms
78,440 KB
testcase_14 AC 308 ms
82,372 KB
testcase_15 AC 419 ms
85,548 KB
testcase_16 AC 188 ms
79,312 KB
testcase_17 AC 1,230 ms
117,840 KB
testcase_18 AC 1,233 ms
108,112 KB
testcase_19 AC 1,233 ms
107,668 KB
testcase_20 AC 1,157 ms
105,380 KB
testcase_21 AC 1,143 ms
106,068 KB
testcase_22 AC 1,017 ms
102,524 KB
testcase_23 AC 1,018 ms
103,460 KB
testcase_24 AC 1,039 ms
103,196 KB
testcase_25 AC 1,268 ms
108,820 KB
testcase_26 AC 1,238 ms
108,840 KB
testcase_27 AC 1,203 ms
108,496 KB
testcase_28 AC 1,262 ms
108,588 KB
testcase_29 AC 1,422 ms
120,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
H = N
W = N
S = [[0] * W for _ in range(H)]
for i in range(M):
    h,w,c = map(int,input().split())
    S[h-1][w-1] = c
import heapq
inf = 10 ** 18
dist = [[[inf] * 2 for _ in range(W)] for _ in range(H)]
q  = [(0,0,0,0)]
dist[0][0][0] = 0
while q:
    d,h,w,flag = heapq.heappop(q)
    if dist[h][w][flag] < d:continue
    if h == N-1 and w == N -1:break
    for i,j in [(1,0),(-1,0),(0,1),(0,-1)]:
        if 0 <= h + i < H and 0 <= w + j < W:
            if S[h+i][w+j] == 0:
                if dist[h+i][w+j][flag] > d + 1:
                    dist[h+i][w+j][flag] = d + 1
                    heapq.heappush(q,(d+1,h+i,w+j,flag))
            else:
                c = S[h+i][w+j]
                if dist[h+i][w+j][flag] > d + 1 + c:
                    dist[h+i][w+j][flag] = d + 1 + c
                    heapq.heappush(q,(d+1+c,h+i,w+j,flag))
                if flag == 0:
                    if dist[h+i][w+j][1] > d + 1:
                        dist[h+i][w+j][1] = d + 1
                        heapq.heappush(q,(d+1,h+i,w+j,1))
print(dist[-1][-1][1])
0