結果

問題 No.1283 Extra Fee
ユーザー 👑 KazunKazun
提出日時 2020-11-07 00:14:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,172 ms / 2,000 ms
コード長 1,207 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 108,672 KB
最終ジャッジ日時 2024-04-27 23:29:38
合計ジャッジ時間 12,089 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,060 KB
testcase_01 AC 33 ms
53,216 KB
testcase_02 AC 33 ms
53,572 KB
testcase_03 AC 34 ms
53,016 KB
testcase_04 AC 41 ms
53,624 KB
testcase_05 AC 35 ms
54,116 KB
testcase_06 AC 36 ms
53,404 KB
testcase_07 AC 34 ms
52,812 KB
testcase_08 AC 37 ms
54,216 KB
testcase_09 AC 36 ms
54,056 KB
testcase_10 AC 32 ms
54,000 KB
testcase_11 AC 184 ms
78,880 KB
testcase_12 AC 132 ms
77,936 KB
testcase_13 AC 111 ms
77,828 KB
testcase_14 AC 162 ms
79,424 KB
testcase_15 AC 204 ms
80,828 KB
testcase_16 AC 116 ms
77,316 KB
testcase_17 AC 1,032 ms
107,140 KB
testcase_18 AC 596 ms
100,520 KB
testcase_19 AC 628 ms
100,576 KB
testcase_20 AC 578 ms
98,992 KB
testcase_21 AC 597 ms
99,456 KB
testcase_22 AC 535 ms
96,824 KB
testcase_23 AC 824 ms
89,688 KB
testcase_24 AC 905 ms
97,340 KB
testcase_25 AC 618 ms
101,740 KB
testcase_26 AC 649 ms
101,488 KB
testcase_27 AC 640 ms
101,276 KB
testcase_28 AC 638 ms
101,548 KB
testcase_29 AC 1,172 ms
108,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    N,M=map(int,input().split())
    inf=float("inf")

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

    V=[(1,0),(-1,0),(0,1),(0,-1)]

    Dist0=[[inf]*(N+1) for __ in range(N+1)]
    Dist1=[[inf]*(N+1) for __ in range(N+1)]
    Dist0[1][1]=0

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

    while Q:
        d,v=heapq.heappop(Q)

        i,j,m=v
        if (m==0 and d<Dist0[i][j]) or (m==1 and d<Dist1[i][j]):
            continue

        if i==j==N:
            break

        for (a,b) in V:
            if 1<=i+a<=N and 1<=j+b<=N:
                c=d+F[i+a][j+b]
                if m==0 and c<Dist0[i+a][j+b]:
                    Dist0[i+a][j+b]=c
                    heapq.heappush(Q,(c,(i+a,j+b,0)))

                if m==1 and c<Dist1[i+a][j+b]:
                    Dist1[i+a][j+b]=c
                    heapq.heappush(Q,(c,(i+a,j+b,1)))

                if m==0 and d+1<Dist1[i+a][j+b]:
                    Dist1[i+a][j+b]=d+1
                    heapq.heappush(Q,(d+1,(i+a,j+b,1)))

    print(min(Dist0[N][N],Dist1[N][N]))

if __name__=="__main__":
    main()
0