結果

問題 No.1283 Extra Fee
ユーザー AEn
提出日時 2022-09-04 14:33:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,257 ms / 2,000 ms
コード長 1,271 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 107,460 KB
最終ジャッジ日時 2024-11-18 19:39:37
合計ジャッジ時間 14,769 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    N, M = map(int,input().split())

    F=[[1]*(N+1) for _ in range(N+1)]
    for _ in range(M):
        h ,w, c = map(int,input().split())
        F[h][w] += c

    xx = [1, 0, -1, 0]
    yy = [0, 1, 0, -1]

    dis0=[[float('inf')]*(N+1) for __ in range(N+1)]
    dis1=[[float('inf')]*(N+1) for __ in range(N+1)]
    dis0[1][1]=0

    Q=[(0,(1,1,0))]
    heapify(Q)

    while Q:
        d, v = heappop(Q)
        i , j, m = v
        if (m==0 and d<dis0[i][j]) or (m==1 and d<dis1[i][j]):
            continue
        if i==j==N:
            break
        for k in range(4):
            cx = i+xx[k]
            cy = j+yy[k]
            if 1<=cx<=N and 1<=cy<=N:
                c = d+F[cx][cy]
                if m==0 and c<dis0[cx][cy]:
                    dis0[cx][cy] = c
                    heappush(Q, (c,(cx,cy,0)))
                if m==1 and c<dis1[cx][cy]:
                    dis1[cx][cy] = c
                    heappush(Q, (c,(cx,cy,1)))
                if m==0 and d+1<dis1[cx][cy]:
                    dis1[cx][cy] = d+1
                    heappush(Q, (d+1,(cx,cy,1)))
    print(min(dis0[N][N], dis1[N][N]))
if __name__=="__main__":
    main()
0