結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
uni_python
|
| 提出日時 | 2020-11-06 22:44:10 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,496 ms / 2,000 ms |
| コード長 | 1,570 bytes |
| コンパイル時間 | 167 ms |
| コンパイル使用メモリ | 82,308 KB |
| 実行使用メモリ | 134,900 KB |
| 最終ジャッジ日時 | 2024-11-16 06:48:15 |
| 合計ジャッジ時間 | 20,678 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
import sys
input=sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
"""
i番目の関所で権利を使うとして,
そこまでのコスト+そこからのコスト
関所を通らない時もあるので,素の最短距離も出しておく
"""
N,M=MI()
from collections import defaultdict
dd = defaultdict(int)
C=[]
for _ in range(M):
h,w,c=MI()
h-=1
w-=1
v=h*N+w
C.append((v,c))
dd[v]=c
C.sort()
def dec(v):
i,j=divmod(v,N)
return i,j
dx=[0,0,1,-1]
dy=[1,-1,0,0]
###
import heapq
inf=float("inf")
def calc(st):
used=[0]*(N**2)
d=[inf]*(N**2)
d[st]=0
S=0#usedの和
hq=[(0,st)]#見た頂点の(最短経路長さ,番号)
while hq:
t,v=heapq.heappop(hq)
if used[v]:
continue
used[v]=1
S+=1
i,j=dec(v)
for k in range(4):
ni=i+dx[k]
nj=j+dy[k]
if 0<=ni<N and 0<=nj<N:
nv=ni*N+nj
if used[nv]:
continue
cost=dd[nv]+1
dtemp=d[v]+cost
if d[nv]<=dtemp:
continue
d[nv]=dtemp
heapq.heappush(hq,(dtemp,nv))
return d
###
dgo=calc(0)
dba=calc(N*N -1)
ans=dgo[-1]
# print(ans)
for v,c in C:
temp=dgo[v]+dba[v]-c*2
# print(v,c,temp)
ans=min(ans,temp)
print(ans)
uni_python