結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー qibqib
提出日時 2022-11-18 18:41:25
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,062 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 147,608 KB
最終ジャッジ日時 2024-09-20 00:02:19
合計ジャッジ時間 31,500 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
62,364 KB
testcase_01 AC 38 ms
55,056 KB
testcase_02 AC 254 ms
80,936 KB
testcase_03 AC 492 ms
81,208 KB
testcase_04 AC 268 ms
81,356 KB
testcase_05 AC 161 ms
79,240 KB
testcase_06 AC 509 ms
80,896 KB
testcase_07 AC 237 ms
80,176 KB
testcase_08 AC 483 ms
80,236 KB
testcase_09 AC 282 ms
82,608 KB
testcase_10 AC 510 ms
81,180 KB
testcase_11 AC 515 ms
81,108 KB
testcase_12 AC 1,563 ms
138,244 KB
testcase_13 AC 577 ms
109,956 KB
testcase_14 AC 1,201 ms
124,804 KB
testcase_15 AC 995 ms
117,068 KB
testcase_16 AC 1,380 ms
125,336 KB
testcase_17 AC 2,220 ms
144,048 KB
testcase_18 AC 2,231 ms
147,380 KB
testcase_19 AC 1,721 ms
137,792 KB
testcase_20 AC 2,332 ms
144,020 KB
testcase_21 AC 2,482 ms
147,608 KB
testcase_22 AC 630 ms
108,872 KB
testcase_23 AC 1,728 ms
137,760 KB
testcase_24 AC 517 ms
115,096 KB
testcase_25 AC 1,638 ms
147,352 KB
testcase_26 TLE -
権限があれば一括ダウンロードができます

ソースコード

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

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))

q = int(input())
for i in range(q):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  l = lca(u, v)
  ans = train[u] + train[v] - 2 * train[l]
  if k > 0:
    ans = min(ans, min(air[j][u] + air[j][v] + ps[j] for j in range(k)))
  print(ans)
0