結果

問題 No.1283 Extra Fee
ユーザー marroncastlemarroncastle
提出日時 2020-11-06 22:48:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,059 ms / 2,000 ms
コード長 1,624 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 173,232 KB
最終ジャッジ日時 2024-04-27 23:25:29
合計ジャッジ時間 16,198 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,908 KB
testcase_01 AC 38 ms
55,248 KB
testcase_02 AC 44 ms
54,788 KB
testcase_03 AC 44 ms
56,240 KB
testcase_04 AC 40 ms
55,312 KB
testcase_05 AC 37 ms
55,376 KB
testcase_06 AC 46 ms
62,352 KB
testcase_07 AC 40 ms
55,200 KB
testcase_08 AC 43 ms
61,860 KB
testcase_09 AC 39 ms
55,864 KB
testcase_10 AC 42 ms
56,528 KB
testcase_11 AC 213 ms
81,984 KB
testcase_12 AC 213 ms
82,792 KB
testcase_13 AC 178 ms
81,284 KB
testcase_14 AC 282 ms
92,820 KB
testcase_15 AC 358 ms
99,040 KB
testcase_16 AC 181 ms
82,608 KB
testcase_17 AC 956 ms
165,284 KB
testcase_18 AC 993 ms
163,372 KB
testcase_19 AC 1,013 ms
170,884 KB
testcase_20 AC 985 ms
162,116 KB
testcase_21 AC 1,033 ms
163,320 KB
testcase_22 AC 910 ms
152,456 KB
testcase_23 AC 871 ms
164,392 KB
testcase_24 AC 918 ms
172,640 KB
testcase_25 AC 1,059 ms
173,096 KB
testcase_26 AC 1,038 ms
173,164 KB
testcase_27 AC 1,021 ms
173,068 KB
testcase_28 AC 1,029 ms
173,232 KB
testcase_29 AC 1,020 ms
172,556 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