結果

問題 No.1283 Extra Fee
ユーザー marroncastlemarroncastle
提出日時 2020-11-06 22:48:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,289 ms / 2,000 ms
コード長 1,624 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 175,432 KB
最終ジャッジ日時 2023-08-10 06:07:37
合計ジャッジ時間 20,662 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,736 KB
testcase_01 AC 89 ms
71,812 KB
testcase_02 AC 87 ms
71,904 KB
testcase_03 AC 91 ms
71,632 KB
testcase_04 AC 89 ms
71,948 KB
testcase_05 AC 89 ms
71,616 KB
testcase_06 AC 99 ms
76,484 KB
testcase_07 AC 89 ms
71,812 KB
testcase_08 AC 97 ms
76,312 KB
testcase_09 AC 92 ms
71,740 KB
testcase_10 AC 89 ms
71,736 KB
testcase_11 AC 273 ms
83,312 KB
testcase_12 AC 282 ms
85,132 KB
testcase_13 AC 244 ms
83,004 KB
testcase_14 AC 369 ms
94,180 KB
testcase_15 AC 453 ms
101,628 KB
testcase_16 AC 245 ms
83,848 KB
testcase_17 AC 1,182 ms
168,748 KB
testcase_18 AC 1,199 ms
166,176 KB
testcase_19 AC 1,216 ms
169,484 KB
testcase_20 AC 1,162 ms
165,456 KB
testcase_21 AC 1,192 ms
165,584 KB
testcase_22 AC 1,082 ms
154,904 KB
testcase_23 AC 1,035 ms
166,312 KB
testcase_24 AC 1,121 ms
171,684 KB
testcase_25 AC 1,259 ms
171,748 KB
testcase_26 AC 1,275 ms
172,192 KB
testcase_27 AC 1,289 ms
172,000 KB
testcase_28 AC 1,272 ms
171,732 KB
testcase_29 AC 1,249 ms
175,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#O(ElogV),重みが整数の場合のみ
import heapq
def dijkstra_fast(s,N,edge,mod):
  dists = [float('inf')] * N #始点sから各頂点への最短距離
  used = [False] * N
  dists[s] = 0
  used[s] = True
  vlist = []
  #vlist : [sからの暫定(未確定)最短距離,頂点]のリスト
  #edge[s] : sから出る枝の[重み,終点]のリスト
  for w,v in edge[s]:
    heapq.heappush(vlist,w*mod+v) #sの隣の点は枝の重さがそのまま暫定最短距離となる
  while len(vlist):
    #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
    minedge = heapq.heappop(vlist)
    #minedge : sからの(確定)最短距離*mod+頂点
    d,v = divmod(minedge,mod)
    if used[v]:
      continue
    dists[v] = d
    used[v] = True
    for d,w in edge[v]:
      if not used[w]:
        heapq.heappush(vlist,(dists[v]+d)*mod+w)
  return dists

N, M = map(int, input().split())
from collections import defaultdict
d = defaultdict(lambda: 1)
for i in range(M):
  h,w,c = map(int, input().split())
  d[(h-1)*N+w-1] = 1+c
edge = [[] for _ in range(N*N)]
for h in range(N):
  for w in range(N):
    if h>0:
      to = (h-1)*N+w
      edge[h*N+w].append((d[to], to))
    if w>0:
      to = h*N+w-1
      edge[h*N+w].append((d[to], to))
    if h<N-1:
      to = (h+1)*N+w
      edge[h*N+w].append((d[to], to))
    if w<N-1:
      to = h*N+w+1
      edge[h*N+w].append((d[to], to))
dist1 = dijkstra_fast(0, N*N, edge, 10**6)
dist2 = dijkstra_fast(N*N-1, N*N, edge, 10**6)
ans = float('inf')
for i in range(1,N*N-1):
  ans = min(ans, dist1[i]+dist2[i]-(d[i]-1)*2)
print(ans)
0