結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー qibqib
提出日時 2022-11-18 18:37:53
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,179 bytes
コンパイル時間 141 ms
コンパイル使用メモリ 81,980 KB
実行使用メモリ 122,544 KB
最終ジャッジ日時 2024-09-19 23:59:21
合計ジャッジ時間 7,672 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 139 ms
79,152 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 433 ms
119,732 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 464 ms
118,580 KB
testcase_23 RE -
testcase_24 AC 384 ms
122,544 KB
testcase_25 RE -
testcase_26 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import heapq

INF = 1 << 60

n, k = map(int, input().split())
g = [[] for _ in range(n + k)]
for _ in range(n - 1):
  a, b, c = map(int, input().split())
  a -= 1
  b -= 1
  g[a].append((b, c))
  g[b].append((a, c))

src = 0
depth = [INF for _ in range(n)]
depth[src] = 0
dq = deque()
dq.appendleft(src)
m = n.bit_length()
doub = [[INF for _ in range(n)] for _ in range(m)]
doub[0][src] = src
train = [INF for _ in range(n)]
train[src] = 0
while len(dq) > 0:
  cur = dq.pop()
  for nxt, c in g[cur]:
    if depth[nxt] < INF:
      continue
    depth[nxt] = depth[cur] + 1
    train[nxt] = train[cur] + c
    doub[0][nxt] = cur
    dq.appendleft(nxt)

for i in range(1, m):
  for v in range(n):
    doub[i][v] = doub[i - 1][doub[i - 1][v]]

def lca(u, v):
  ut = u
  vt = v

  if depth[ut] > depth[vt]:
    ut, vt = vt, ut

  diff = depth[vt] - depth[ut]
  for i in range(m):
    if (diff >> i) & 1 != 0:
      vt = doub[i][vt]

  if ut != vt:
    for i in range(depth[ut].bit_length() - 1, -1, -1):
      if doub[i][ut] != doub[i][vt]:
        ut = doub[i][ut]
        vt = doub[i][vt]

    if ut != vt:
      ut = doub[0][ut]

  return ut

q = int(input())
queries = []
ans = [0 for _ in range(q)]
for i in range(q):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  queries.append((u, v))
  l = lca(u, v)
  ans[i] = train[u] + train[v] - 2 * train[l]

if k > 0:
  air = [[INF for _ in range(n + k)] for _ in range(k)]
  ps = [0 for _ in range(k)]
  for i in range(k):
    _, p = map(int, input().split())
    ps[i] = p
    xs = list(map(int, input().split()))
    for x in xs:
      x -= 1
      g[n + i].append((x, 0))
      g[x].append((n + i, p))

  for i in range(k):
    src = n + i
    air[i][src] = 0
    hp = [(0, src)]
    while len(hp) > 0:
      cd, cur = heapq.heappop(hp)
      if air[i][cur] > cd:
        continue
      for nxt, cost in g[cur]:
        if air[i][nxt] > air[i][cur] + cost:
          air[i][nxt] = air[i][cur] + cost
          heapq.heappush(hp, (air[i][nxt], nxt))

  for i in range(q):
    u, v = queries[i]
    ans[i] = min(ans[i], min(air[j][u] + air[j][v] + ps[j] for j in range(k)))

print(*ans, sep="\n")
0