結果

問題 No.1283 Extra Fee
ユーザー titiatitia
提出日時 2020-11-06 22:39:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,494 ms / 2,000 ms
コード長 1,087 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 82,656 KB
実行使用メモリ 102,552 KB
最終ジャッジ日時 2024-04-27 23:22:43
合計ジャッジ時間 16,858 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,960 KB
testcase_01 AC 35 ms
53,404 KB
testcase_02 AC 36 ms
53,516 KB
testcase_03 AC 37 ms
53,940 KB
testcase_04 AC 34 ms
53,236 KB
testcase_05 AC 35 ms
53,624 KB
testcase_06 AC 36 ms
54,560 KB
testcase_07 AC 35 ms
53,336 KB
testcase_08 AC 41 ms
54,468 KB
testcase_09 AC 41 ms
54,220 KB
testcase_10 AC 40 ms
53,496 KB
testcase_11 AC 215 ms
79,752 KB
testcase_12 AC 250 ms
79,112 KB
testcase_13 AC 171 ms
77,944 KB
testcase_14 AC 291 ms
79,680 KB
testcase_15 AC 397 ms
81,288 KB
testcase_16 AC 170 ms
78,212 KB
testcase_17 AC 1,364 ms
101,932 KB
testcase_18 AC 978 ms
90,544 KB
testcase_19 AC 1,021 ms
90,840 KB
testcase_20 AC 848 ms
88,880 KB
testcase_21 AC 942 ms
89,668 KB
testcase_22 AC 788 ms
87,292 KB
testcase_23 AC 804 ms
85,672 KB
testcase_24 AC 824 ms
85,040 KB
testcase_25 AC 959 ms
90,040 KB
testcase_26 AC 1,000 ms
91,092 KB
testcase_27 AC 1,010 ms
90,768 KB
testcase_28 AC 996 ms
90,996 KB
testcase_29 AC 1,494 ms
102,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

DP=[[1<<60]*N for i in range(N)]
DP2=[[1<<60]*N for i in range(N)]
MAP=[[1]*N for i in range(N)]

for i in range(M):
    h,w,c=map(int,input().split())
    h-=1
    w-=1
    MAP[h][w]+=c

DP[0][0]=0

import heapq

# Q=[(c,x,y,f)]
Q=[(0,0,0,0)]

while Q:
    c,x,y,f=heapq.heappop(Q)

    if f==0 and c>DP[x][y]:
        continue
    if f==1 and c>DP2[x][y]:
        continue

    if f==1:
        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=z<N and 0<=w<N and c+MAP[z][w]<DP2[z][w]:
                DP2[z][w]=c+MAP[z][w]
                heapq.heappush(Q,(DP2[z][w],z,w,1))

    else:
        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=z<N and 0<=w<N and c+MAP[z][w]<DP[z][w]:
                DP[z][w]=c+MAP[z][w]
                heapq.heappush(Q,(DP[z][w],z,w,0))

            if 0<=z<N and 0<=w<N and MAP[z][w]!=1 and c+1<DP2[z][w]:
                DP2[z][w]=c+1
                heapq.heappush(Q,(DP2[z][w],z,w,1))
        
print(min(DP[N-1][N-1],DP2[N-1][N-1]))
            
0