結果

問題 No.1283 Extra Fee
ユーザー ygd.ygd.
提出日時 2020-11-06 23:14:52
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 135,476 KB
最終ジャッジ日時 2023-09-29 19:33:16
合計ジャッジ時間 5,625 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,292 KB
testcase_01 AC 76 ms
71,220 KB
testcase_02 AC 85 ms
75,532 KB
testcase_03 AC 78 ms
71,192 KB
testcase_04 AC 78 ms
71,324 KB
testcase_05 AC 78 ms
71,288 KB
testcase_06 AC 137 ms
77,396 KB
testcase_07 AC 78 ms
71,364 KB
testcase_08 AC 139 ms
77,768 KB
testcase_09 AC 123 ms
77,304 KB
testcase_10 AC 117 ms
77,112 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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