結果

問題 No.1283 Extra Fee
ユーザー AEnAEn
提出日時 2022-08-31 14:20:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,644 ms / 2,000 ms
コード長 1,183 bytes
コンパイル時間 483 ms
コンパイル使用メモリ 82,828 KB
実行使用メモリ 109,332 KB
最終ジャッジ日時 2024-04-25 18:40:27
合計ジャッジ時間 20,581 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
54,232 KB
testcase_01 AC 36 ms
53,552 KB
testcase_02 AC 34 ms
53,132 KB
testcase_03 AC 34 ms
54,304 KB
testcase_04 AC 36 ms
53,216 KB
testcase_05 AC 37 ms
53,600 KB
testcase_06 AC 40 ms
55,396 KB
testcase_07 AC 36 ms
53,624 KB
testcase_08 AC 40 ms
55,056 KB
testcase_09 AC 38 ms
54,516 KB
testcase_10 AC 38 ms
54,632 KB
testcase_11 AC 222 ms
79,808 KB
testcase_12 AC 210 ms
78,952 KB
testcase_13 AC 159 ms
77,988 KB
testcase_14 AC 297 ms
80,384 KB
testcase_15 AC 416 ms
82,616 KB
testcase_16 AC 189 ms
78,796 KB
testcase_17 AC 1,522 ms
107,428 KB
testcase_18 AC 1,224 ms
102,340 KB
testcase_19 AC 1,283 ms
102,376 KB
testcase_20 AC 1,175 ms
100,540 KB
testcase_21 AC 1,200 ms
101,384 KB
testcase_22 AC 1,066 ms
97,864 KB
testcase_23 AC 918 ms
90,240 KB
testcase_24 AC 1,037 ms
97,952 KB
testcase_25 AC 1,359 ms
103,188 KB
testcase_26 AC 1,259 ms
103,272 KB
testcase_27 AC 1,292 ms
103,848 KB
testcase_28 AC 1,285 ms
103,764 KB
testcase_29 AC 1,644 ms
109,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from heapq import heappush,heappop

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

dis1 = [[float('inf') for _ in range(N)] for _ in range(N)]
dis2 = [[float('inf') for _ in range(N)] for _ in range(N)]
dis1[0][0] = 0
dis2[0][0] = 0
p = [(0,0,0,0)]
xx = [0,1,0,-1]
yy = [1,0,-1,0]
while p:
    c, x, y, f = heappop(p)
    if f==0 and dis1[x][y]<c:
        continue
    if f==1 and dis2[x][y]<c:
        continue
    for px, py in zip(xx, yy):
        cx = x+px
        cy = y+py
        if 0<=cx<N and 0<=cy<N:
            cost1 = c+cost[cx][cy]+1
            cost2 = c+1
            if f:
                if dis2[cx][cy]>cost1:
                    dis2[cx][cy] = cost1
                    heappush(p, (cost1, cx, cy, f))
            else:
                if dis1[cx][cy]>cost1:
                    dis1[cx][cy] = cost1
                    heappush(p, (cost1, cx, cy, f))
                if dis2[cx][cy]>cost2:
                    dis2[cx][cy] = cost2
                    heappush(p, (cost2, cx, cy, f+1))

print(min(dis1[-1][-1],dis2[-1][-1]))
0