結果

問題 No.1283 Extra Fee
ユーザー ああいいああいい
提出日時 2022-02-19 14:12:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,331 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 121,056 KB
最終ジャッジ日時 2024-04-28 00:48:45
合計ジャッジ時間 18,567 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,608 KB
testcase_01 AC 37 ms
52,608 KB
testcase_02 AC 38 ms
52,864 KB
testcase_03 AC 36 ms
52,480 KB
testcase_04 AC 35 ms
52,736 KB
testcase_05 AC 34 ms
52,480 KB
testcase_06 AC 35 ms
52,992 KB
testcase_07 AC 36 ms
52,608 KB
testcase_08 AC 38 ms
53,888 KB
testcase_09 AC 40 ms
53,248 KB
testcase_10 AC 40 ms
52,992 KB
testcase_11 AC 192 ms
79,600 KB
testcase_12 AC 217 ms
79,620 KB
testcase_13 AC 165 ms
77,952 KB
testcase_14 AC 301 ms
82,508 KB
testcase_15 AC 410 ms
85,680 KB
testcase_16 AC 187 ms
79,032 KB
testcase_17 AC 1,226 ms
117,696 KB
testcase_18 AC 1,231 ms
107,600 KB
testcase_19 AC 1,216 ms
107,540 KB
testcase_20 AC 1,116 ms
105,388 KB
testcase_21 AC 1,089 ms
106,040 KB
testcase_22 AC 1,000 ms
102,616 KB
testcase_23 AC 1,006 ms
103,060 KB
testcase_24 AC 983 ms
103,552 KB
testcase_25 AC 1,226 ms
108,692 KB
testcase_26 AC 1,234 ms
108,328 KB
testcase_27 AC 1,206 ms
108,288 KB
testcase_28 AC 1,208 ms
108,984 KB
testcase_29 AC 1,331 ms
121,056 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