結果

問題 No.1283 Extra Fee
ユーザー 👑 KazunKazun
提出日時 2021-02-10 18:40:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 714 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 115,764 KB
最終ジャッジ日時 2024-04-27 23:48:31
合計ジャッジ時間 11,059 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,992 KB
testcase_01 AC 38 ms
52,864 KB
testcase_02 AC 38 ms
53,504 KB
testcase_03 AC 37 ms
52,864 KB
testcase_04 AC 35 ms
53,248 KB
testcase_05 AC 35 ms
53,248 KB
testcase_06 AC 36 ms
53,632 KB
testcase_07 AC 35 ms
53,504 KB
testcase_08 AC 38 ms
54,016 KB
testcase_09 AC 35 ms
53,504 KB
testcase_10 AC 36 ms
53,632 KB
testcase_11 AC 150 ms
78,984 KB
testcase_12 AC 156 ms
78,012 KB
testcase_13 AC 129 ms
77,492 KB
testcase_14 AC 186 ms
79,544 KB
testcase_15 AC 245 ms
81,700 KB
testcase_16 AC 145 ms
77,828 KB
testcase_17 AC 573 ms
113,664 KB
testcase_18 AC 676 ms
104,104 KB
testcase_19 AC 681 ms
105,196 KB
testcase_20 AC 616 ms
102,184 KB
testcase_21 AC 652 ms
102,708 KB
testcase_22 AC 596 ms
100,180 KB
testcase_23 AC 447 ms
90,688 KB
testcase_24 AC 535 ms
98,956 KB
testcase_25 AC 700 ms
105,788 KB
testcase_26 AC 679 ms
105,256 KB
testcase_27 AC 670 ms
105,128 KB
testcase_28 AC 714 ms
105,932 KB
testcase_29 AC 598 ms
115,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import  heappop,heappush
class X:
        def __init__(self,x,d):
            self.d=d
            self.x=x

        def __str__(self):
            return "{}:{}".forma(self.x,self.d)

        def __repr__(self):
            return str(self)

        def __lt__(self,other):
            return self.d<other.d

        def __iter__(self,other):
            yield from (self.x,self.d)

class Dijkstra_Heap:
    def __init__(self):
        self.heap=[]

    def __str__(self):
        return str(self.heap)

    def __repr__(self):
        return repr(self.heap)

    def __bool__(self):
        return bool(self.heap)

    def push(self,point,dist):
        heappush(self.heap,X(point,dist))

    def pop(self):
        t=heappop(self.heap)
        return t.x,t.d

def main():
    import sys
    from heapq import heappop,heappush
    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

    H=Dijkstra_Heap()
    H.push((1,1,0),0)

    while H:
        v,d=H.pop()

        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
                    H.push((i+a,j+b,0),c)

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

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

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

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