結果

問題 No.1283 Extra Fee
ユーザー DrDrpilotDrDrpilot
提出日時 2022-03-15 13:09:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,796 ms / 2,000 ms
コード長 1,013 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 121,636 KB
最終ジャッジ日時 2023-10-22 03:33:53
合計ジャッジ時間 8,888 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,460 KB
testcase_01 AC 35 ms
53,404 KB
testcase_02 AC 36 ms
53,404 KB
testcase_03 AC 35 ms
53,404 KB
testcase_04 AC 34 ms
53,404 KB
testcase_05 AC 35 ms
53,404 KB
testcase_06 AC 38 ms
53,404 KB
testcase_07 AC 36 ms
53,404 KB
testcase_08 AC 39 ms
55,452 KB
testcase_09 AC 36 ms
53,404 KB
testcase_10 AC 37 ms
53,404 KB
testcase_11 AC 267 ms
79,768 KB
testcase_12 AC 217 ms
79,156 KB
testcase_13 AC 161 ms
77,824 KB
testcase_14 AC 317 ms
82,132 KB
testcase_15 AC 403 ms
84,692 KB
testcase_16 AC 186 ms
78,492 KB
testcase_17 AC 1,653 ms
118,572 KB
testcase_18 AC 1,277 ms
107,016 KB
testcase_19 AC 1,259 ms
107,388 KB
testcase_20 AC 1,191 ms
105,608 KB
testcase_21 AC 1,196 ms
105,860 KB
testcase_22 AC 1,065 ms
102,040 KB
testcase_23 AC 1,036 ms
102,880 KB
testcase_24 AC 1,029 ms
103,020 KB
testcase_25 AC 1,303 ms
108,436 KB
testcase_26 AC 1,290 ms
108,008 KB
testcase_27 AC 1,262 ms
108,160 KB
testcase_28 AC 1,345 ms
107,956 KB
testcase_29 AC 1,796 ms
121,636 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
    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