結果

問題 No.1283 Extra Fee
ユーザー AEnAEn
提出日時 2022-08-31 14:23:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,816 ms / 2,000 ms
コード長 1,418 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 108,812 KB
最終ジャッジ日時 2023-08-10 07:15:56
合計ジャッジ時間 21,810 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,256 KB
testcase_01 AC 74 ms
71,296 KB
testcase_02 AC 72 ms
71,328 KB
testcase_03 AC 70 ms
71,072 KB
testcase_04 AC 72 ms
71,156 KB
testcase_05 AC 72 ms
71,156 KB
testcase_06 AC 74 ms
71,444 KB
testcase_07 AC 73 ms
71,408 KB
testcase_08 AC 77 ms
71,216 KB
testcase_09 AC 78 ms
71,072 KB
testcase_10 AC 78 ms
71,384 KB
testcase_11 AC 252 ms
80,824 KB
testcase_12 AC 247 ms
80,612 KB
testcase_13 AC 187 ms
79,224 KB
testcase_14 AC 339 ms
81,340 KB
testcase_15 AC 448 ms
83,584 KB
testcase_16 AC 216 ms
79,252 KB
testcase_17 AC 1,646 ms
107,996 KB
testcase_18 AC 1,274 ms
102,788 KB
testcase_19 AC 1,343 ms
103,272 KB
testcase_20 AC 1,247 ms
101,636 KB
testcase_21 AC 1,181 ms
100,904 KB
testcase_22 AC 1,083 ms
99,348 KB
testcase_23 AC 997 ms
91,200 KB
testcase_24 AC 1,082 ms
98,980 KB
testcase_25 AC 1,301 ms
103,020 KB
testcase_26 AC 1,321 ms
103,268 KB
testcase_27 AC 1,362 ms
104,244 KB
testcase_28 AC 1,378 ms
103,764 KB
testcase_29 AC 1,816 ms
108,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    from heapq import heappush,heappop
    input = sys.stdin.readline

    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]))
if __name__=='__main__':
    main()
0