結果

問題 No.1283 Extra Fee
ユーザー AEnAEn
提出日時 2022-09-04 14:15:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,773 ms / 2,000 ms
コード長 1,404 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 102,556 KB
最終ジャッジ日時 2024-04-29 14:29:45
合計ジャッジ時間 20,399 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
52,480 KB
testcase_01 AC 43 ms
52,480 KB
testcase_02 AC 43 ms
52,864 KB
testcase_03 AC 41 ms
52,480 KB
testcase_04 AC 42 ms
52,736 KB
testcase_05 AC 41 ms
52,608 KB
testcase_06 AC 44 ms
52,864 KB
testcase_07 AC 43 ms
52,736 KB
testcase_08 AC 44 ms
52,992 KB
testcase_09 AC 44 ms
53,120 KB
testcase_10 AC 44 ms
52,736 KB
testcase_11 AC 233 ms
78,708 KB
testcase_12 AC 229 ms
78,168 KB
testcase_13 AC 166 ms
77,824 KB
testcase_14 AC 311 ms
79,156 KB
testcase_15 AC 418 ms
80,128 KB
testcase_16 AC 185 ms
77,592 KB
testcase_17 AC 1,612 ms
101,136 KB
testcase_18 AC 1,169 ms
90,732 KB
testcase_19 AC 1,187 ms
90,252 KB
testcase_20 AC 1,112 ms
89,116 KB
testcase_21 AC 1,056 ms
88,836 KB
testcase_22 AC 949 ms
87,356 KB
testcase_23 AC 945 ms
85,248 KB
testcase_24 AC 956 ms
85,100 KB
testcase_25 AC 1,137 ms
89,748 KB
testcase_26 AC 1,183 ms
90,488 KB
testcase_27 AC 1,186 ms
90,176 KB
testcase_28 AC 1,205 ms
90,524 KB
testcase_29 AC 1,773 ms
102,556 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 = [[10**15 for _ in range(N)] for _ in range(N)]
    dis2 = [[10**15 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 i in range(4):
            cx = x+xx[i]
            cy = y+yy[i]
            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