結果

問題 No.1283 Extra Fee
ユーザー DrDrpilotDrDrpilot
提出日時 2022-03-15 13:10:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,518 ms / 2,000 ms
コード長 1,045 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 120,228 KB
最終ジャッジ日時 2024-04-28 00:50:27
合計ジャッジ時間 20,791 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,352 KB
testcase_01 AC 42 ms
52,608 KB
testcase_02 AC 45 ms
52,992 KB
testcase_03 AC 42 ms
52,480 KB
testcase_04 AC 41 ms
52,992 KB
testcase_05 AC 41 ms
52,736 KB
testcase_06 AC 44 ms
53,120 KB
testcase_07 AC 44 ms
52,736 KB
testcase_08 AC 46 ms
53,760 KB
testcase_09 AC 43 ms
53,248 KB
testcase_10 AC 43 ms
53,120 KB
testcase_11 AC 207 ms
79,268 KB
testcase_12 AC 231 ms
79,276 KB
testcase_13 AC 181 ms
78,336 KB
testcase_14 AC 335 ms
82,536 KB
testcase_15 AC 450 ms
85,288 KB
testcase_16 AC 199 ms
78,844 KB
testcase_17 AC 1,396 ms
117,572 KB
testcase_18 AC 1,364 ms
107,788 KB
testcase_19 AC 1,340 ms
108,120 KB
testcase_20 AC 1,290 ms
105,684 KB
testcase_21 AC 1,265 ms
105,936 KB
testcase_22 AC 1,125 ms
102,432 KB
testcase_23 AC 1,128 ms
103,028 KB
testcase_24 AC 1,132 ms
103,652 KB
testcase_25 AC 1,400 ms
108,544 KB
testcase_26 AC 1,381 ms
108,444 KB
testcase_27 AC 1,361 ms
108,344 KB
testcase_28 AC 1,420 ms
108,292 KB
testcase_29 AC 1,518 ms
120,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
n,m=map(int,input().split())
money=[[0]*n for _ in range(n)]
for _ in range(m):
    h,w,c=map(int,input().split())
    money[h-1][w-1]=c
inf=10**18
d=[[[inf]*2 for _ in range(n)] for _ in range(n)]
d[0][0][0]=0
q=[(0,0,0,0)]
while q:
    now_d,y,x,use=heapq.heappop(q)
    if d[y][x][use]<now_d:
        continue
    if y==x==n-1:
        break
    for dy,dx in [(1,0),(0,1),(-1,0),(0,-1)]:
        ny=y+dy;nx=x+dx
        if 0<=ny<n and 0<=nx<n:
            if money[ny][nx]==0:
                if d[ny][nx][use]>now_d+1:
                    d[ny][nx][use]=now_d+1
                    heapq.heappush(q,(now_d+1,ny,nx,use))
            else:
                cost=money[ny][nx]
                if d[ny][nx][use]>now_d+cost+1:
                    d[ny][nx][use]=now_d+cost+1
                    heapq.heappush(q,(now_d+cost+1,ny,nx,use))
                if use==0:
                    if d[ny][nx][1]>now_d+1:
                        d[ny][nx][1]=now_d+1
                        heapq.heappush(q,(now_d+1,ny,nx,1))
print(d[-1][-1][1])
0