結果

問題 No.1283 Extra Fee
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-19 09:40:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,248 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 120,856 KB
最終ジャッジ日時 2024-04-27 23:48:52
合計ジャッジ時間 15,759 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,736 KB
testcase_01 AC 34 ms
52,736 KB
testcase_02 AC 35 ms
52,608 KB
testcase_03 AC 38 ms
52,480 KB
testcase_04 AC 35 ms
52,864 KB
testcase_05 AC 36 ms
52,736 KB
testcase_06 AC 37 ms
53,504 KB
testcase_07 AC 35 ms
52,736 KB
testcase_08 AC 37 ms
53,504 KB
testcase_09 AC 35 ms
53,376 KB
testcase_10 AC 36 ms
52,864 KB
testcase_11 AC 171 ms
78,584 KB
testcase_12 AC 183 ms
79,292 KB
testcase_13 AC 152 ms
78,760 KB
testcase_14 AC 255 ms
83,688 KB
testcase_15 AC 335 ms
87,856 KB
testcase_16 AC 172 ms
78,848 KB
testcase_17 AC 1,139 ms
104,024 KB
testcase_18 AC 924 ms
116,404 KB
testcase_19 AC 948 ms
119,040 KB
testcase_20 AC 925 ms
116,564 KB
testcase_21 AC 911 ms
117,212 KB
testcase_22 AC 812 ms
112,900 KB
testcase_23 AC 842 ms
111,588 KB
testcase_24 AC 933 ms
118,232 KB
testcase_25 AC 1,000 ms
120,856 KB
testcase_26 AC 988 ms
120,608 KB
testcase_27 AC 972 ms
120,740 KB
testcase_28 AC 997 ms
120,484 KB
testcase_29 AC 1,248 ms
104,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop,heappush

def main1(n,m,hwc):  
  # d0[i][j]:(0,0)から(i,j)への最小コスト
  # d1[i][j]:(i,j)から(n-1,n-1)への最小コスト

  cost=[0]*(n**2)
  for h,w,c in hwc:
    h,w=h-1,w-1
    cost[h*n+w]=c

  inf=float('inf')
  d0=[inf]*(n**2)
  d0[0]=0
  todo=[[0,0]]
  
  while todo:
    c,v=heappop(todo)
    if d0[v]<c:continue
    x,y=v//n,v%n
    for dx,dy in ((0,1),(1,0),(0,-1),(-1,0)):
      nx,ny=x+dx,y+dy
      if 0<=nx<n and 0<=ny<n:
        dc=cost[nx*n+ny]+1
        if d0[nx*n+ny]>c+dc:
          d0[nx*n+ny]=c+dc
          heappush(todo,[c+dc,nx*n+ny])
  
  d1=[inf]*(n**2)
  d1[(n-1)*n+n-1]=0
  todo=[[0,(n-1)*n+n-1]]
  while todo:
    c,v=heappop(todo)
    if d1[v]<c:continue
    x,y=v//n,v%n
    for dx,dy in ((0,1),(1,0),(0,-1),(-1,0)):
      nx,ny=x+dx,y+dy
      if 0<=nx<n and 0<=ny<n:
        dc=cost[nx*n+ny]+1
        if d1[nx*n+ny]>c+dc:
          d1[nx*n+ny]=c+dc
          heappush(todo,[c+dc,nx*n+ny])
  ans=inf
  for i in range(n**2):
    ans=min(ans,d1[i]+d0[i]-2*cost[i])
  return ans
if __name__=='__main__':
  n,m=map(int,input().split())
  hwc=[list(map(int,input().split())) for _ in range(m)]
  ret1=main1(n,m,hwc)
  print(ret1)
0