結果

問題 No.1301 Strange Graph Shortest Path
ユーザー marroncastlemarroncastle
提出日時 2020-11-27 23:30:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,658 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 87,288 KB
実行使用メモリ 174,460 KB
最終ジャッジ日時 2023-10-10 21:46:51
合計ジャッジ時間 41,074 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,296 KB
testcase_01 AC 71 ms
71,364 KB
testcase_02 WA -
testcase_03 AC 1,060 ms
154,512 KB
testcase_04 AC 1,229 ms
174,292 KB
testcase_05 AC 1,185 ms
166,076 KB
testcase_06 AC 1,209 ms
168,680 KB
testcase_07 AC 1,197 ms
168,296 KB
testcase_08 AC 1,091 ms
156,012 KB
testcase_09 AC 1,125 ms
161,340 KB
testcase_10 WA -
testcase_11 AC 1,240 ms
170,420 KB
testcase_12 AC 1,223 ms
171,032 KB
testcase_13 AC 1,270 ms
169,608 KB
testcase_14 AC 1,121 ms
159,520 KB
testcase_15 AC 1,198 ms
163,700 KB
testcase_16 AC 1,224 ms
174,460 KB
testcase_17 AC 1,302 ms
170,580 KB
testcase_18 AC 1,214 ms
164,308 KB
testcase_19 AC 1,116 ms
168,272 KB
testcase_20 AC 1,083 ms
167,932 KB
testcase_21 AC 1,260 ms
169,180 KB
testcase_22 AC 1,122 ms
169,384 KB
testcase_23 AC 1,325 ms
170,616 KB
testcase_24 AC 1,162 ms
168,788 KB
testcase_25 AC 1,326 ms
173,832 KB
testcase_26 AC 1,347 ms
168,716 KB
testcase_27 AC 1,261 ms
170,344 KB
testcase_28 AC 1,129 ms
164,020 KB
testcase_29 WA -
testcase_30 AC 1,400 ms
171,492 KB
testcase_31 AC 1,302 ms
171,904 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 1,204 ms
163,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import sys,os,io
input = sys.stdin.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
N, M = map(int, input().split())
edge = [[] for _ in range(N)]
edges = {}
after = {}
for i in range(M):
  u,v,c,d = map(int, input().split())
  u -= 1
  v -= 1
  edge[u].append((v,c))
  edge[v].append((u,c))
  edges[(u,v)] = c
  edges[(v,u)] = c
  after[(u,v)] = d
  after[(v,u)] = d
dists1, prev1 = dijkstra_heap(0, edge)
ans = dists1[-1]
v = N-1
while v!=0:
  w = prev1[v]
  edges[(w,v)] = after[(w,v)]
  edges[(v,w)] = after[(v,w)]
  v = w
new_edge = [[] for _ in range(N)]
for i in range(N):
  for v,c in edge[i]:
    new_edge[i].append((v,edges[(i,v)]))
dists2, prev2 = dijkstra_heap(0,new_edge)
ans += dists2[-1]
print(ans)

0