結果

問題 No.1812 Uribo Road
ユーザー tobusakanatobusakana
提出日時 2022-10-23 21:06:08
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 3,266 bytes
コンパイル時間 677 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 534,180 KB
最終ジャッジ日時 2023-09-15 06:36:22
合計ジャッジ時間 9,977 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,388 KB
testcase_01 AC 76 ms
71,304 KB
testcase_02 AC 87 ms
76,396 KB
testcase_03 AC 578 ms
86,100 KB
testcase_04 AC 75 ms
71,272 KB
testcase_05 AC 77 ms
71,464 KB
testcase_06 AC 87 ms
76,384 KB
testcase_07 AC 180 ms
78,900 KB
testcase_08 AC 249 ms
80,872 KB
testcase_09 AC 297 ms
81,584 KB
testcase_10 AC 254 ms
81,208 KB
testcase_11 AC 215 ms
79,676 KB
testcase_12 MLE -
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 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 本質的には猪のいる道の両端の頂点の情報さえ分かればよい。
# それらの頂点間の最短距離を求める。
# グラフとしては、それらの頂点間だけに辺を貼ったものを作る。

import sys
readline = sys.stdin.readline

N,M,K = map(int,readline().split())
R = list(map(int,readline().split()))
Rdic = {} # 道番号->猪の番号へのdictionary
for i in range(K):
  Rdic[R[i] - 1] = i
  
# まず通常のグラフを作成する。
G = [[] for i in range(N)]
# 重要な頂点
points = set()
points.add(0)
points.add(N - 1)
ino_road = {}
for i in range(M):
  a,b,c = map(int,readline().split())
  a -= 1
  b -= 1
  # イノシシのいる道か?
  ino = -1
  if i in Rdic:
    ino = Rdic[i] # イノシシの番号
    points.add(a) # 重要な頂点として追加
    points.add(b)
    if a > b:
      a,b = b,a
    ino_road[(a, b)] = [c, ino]
  G[a].append([b, c, ino])
  G[b].append([a, c, ino])

points = sorted(points)
L = len(points) # 重要な頂点の数
# 重要な頂点->元の頂点、元の頂点->重要な頂点への変換
# 重要な頂点番号 -> 元の頂点番号への変換はpoints[重要な頂点番号]
Pdic = {}
for i in range(L):
  Pdic[points[i]] = i
# 元の頂点->重要な頂点番号への変換はPdic[元の頂点番号]

import heapq as hq
R = [[] for i in range(L)] # 重要な頂点のみのグラフ
# まず、猪がいる道には必ず道路を引く。
for key, value in ino_road.items():
  a_ind = Pdic[key[0]]
  b_ind = Pdic[key[1]]
  R[a_ind].append([b_ind, value[0], value[1]])
  R[b_ind].append([a_ind, value[0], value[1]])

for start in range(L): # 頂点を変えてのダイクストラ
  s = points[start] # 元のグラフでの重要な頂点番号
  # Gに対してのダイクストラ
  visited = [False] * N # 訪問済みの管理
  q = []
  hq.heappush(q, (0, s)) # コスト、頂点
  while q:
    d, v = hq.heappop(q)
    if visited[v]:
      continue
    visited[v] = True
    if v in Pdic: # 重要な頂点である場合
      imp = Pdic[v] # 重要な頂点番号
      if start != imp and start < imp and (start, imp) not in ino_road:
        R[start].append([imp, d, -1])
        R[imp].append([start, d, -1])
    for child, cost, ino in G[v]:
      if visited[child]:
        continue
      hq.heappush(q, (d + cost, child))
      
# Rが重要な頂点同士のグラフになった。
# あとは、このグラフで
# dp[v][S] = 今いる頂点v, 逢った猪の集合S
# をやる。
start = Pdic[0]
goal = Pdic[N - 1]

INF = 1 << 60
dp = [[INF] * (1 << K) for i in range(N)]

q = []
hq.heappush(q, (0, 0, 0)) # 距離、頂点、今までにあった猪の集合
complete = (1 << K) - 1
while q:
  d, v, ino_status = hq.heappop(q)
  if dp[v][ino_status] != INF:
    continue
  dp[v][ino_status] = d
  if v == goal and ino_status == complete:
    print(d)
    break
  for child, cost, ino in R[v]:
    if ino != -1: # イノシシに出会う場合
      next_status = ino_status | (1 << ino)
      if dp[child][next_status] != INF:
        continue
      hq.heappush(q, (d + cost, child, next_status))
    else:
      if dp[child][ino_status] != INF:
        continue
      hq.heappush(q, (d + cost, child, ino_status))
  
  
  
0