結果

問題 No.1283 Extra Fee
ユーザー ygd.
提出日時 2020-11-06 23:14:52
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 146,256 KB
最終ジャッジ日時 2024-07-22 13:37:16
合計ジャッジ時間 5,088 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11 TLE * 1 -- * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heapify, heappop, heappush
N,M = map(int,input().split()); INF = float("inf")
dic = {}
for i in range(M):
  h,w,c = map(int,input().split())
  h-=1;w-=1
  dic[h*N+w] = c
#print(dic)  
G = [[] for _ in range(N*N)]
dxdy = [(0,1),(0,-1),(1,0),(-1,0)]
for i in range(N):
  for j in range(N):
    now = i*N + j
    for k in range(4):
      ni = i+dxdy[k][0]
      nj = j+dxdy[k][1]
      if 0<=ni<N and 0<=nj<N:
        nxt = ni*N+nj
        if nxt in dic:
          G[now].append((1+dic[nxt],nxt))
        else:
          G[now].append((1,nxt))
#print(G)

def dijkstra_heap2(s,G):
  #S:start, V: node, E: Edge, G: Graph
  V = len(G)
  d = [INF for _ in range(V)]
  d[s] = 0
  PQ = []
  heappush(PQ,(0,s))
  
  while PQ:
    c,v = heappop(PQ)
    if d[v] < c:
      continue
    d[v] = c
    for cost,u in G[v]:
      if d[u] <= cost + d[v]:
        continue
      d[u] = cost + d[v]
      heappush(PQ,(d[u], u))
  
  return d

#どこも引かない。
D = dijkstra_heap2(0,G)
ans = D[-1]
#print(ans)
for x in dic:
  hajime = D[x]-dic[x]
  E = dijkstra_heap2(x,G)
  ato = E[-1]
  temp = hajime + ato
  ans = min(ans,temp)
print(ans)
0