結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー first_vilfirst_vil
提出日時 2021-02-01 00:42:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,115 ms / 3,000 ms
コード長 1,905 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 82,944 KB
実行使用メモリ 147,320 KB
最終ジャッジ日時 2024-04-19 20:13:12
合計ジャッジ時間 22,128 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,784 KB
testcase_01 AC 40 ms
54,912 KB
testcase_02 AC 166 ms
80,008 KB
testcase_03 AC 217 ms
79,632 KB
testcase_04 AC 164 ms
80,332 KB
testcase_05 AC 127 ms
79,016 KB
testcase_06 AC 224 ms
79,704 KB
testcase_07 AC 159 ms
79,304 KB
testcase_08 AC 214 ms
79,372 KB
testcase_09 AC 190 ms
81,076 KB
testcase_10 AC 270 ms
80,056 KB
testcase_11 AC 279 ms
80,236 KB
testcase_12 AC 1,224 ms
133,432 KB
testcase_13 AC 431 ms
108,152 KB
testcase_14 AC 875 ms
123,096 KB
testcase_15 AC 715 ms
114,004 KB
testcase_16 AC 1,103 ms
122,020 KB
testcase_17 AC 1,897 ms
140,544 KB
testcase_18 AC 2,115 ms
143,784 KB
testcase_19 AC 1,456 ms
133,532 KB
testcase_20 AC 2,081 ms
141,284 KB
testcase_21 AC 1,945 ms
141,680 KB
testcase_22 AC 440 ms
122,368 KB
testcase_23 AC 1,335 ms
147,320 KB
testcase_24 AC 316 ms
109,656 KB
testcase_25 AC 1,195 ms
144,660 KB
testcase_26 AC 457 ms
125,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
  import sys
  input=sys.stdin.buffer.readline
  
  from heapq import heappush,heappop
  
  from collections import deque
  
  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))
  
  logn=17
  dep=[0]*n
  dis=[1<<60]*n
  nxt=[[-1]*n for _ in range(logn)]
  stk=deque([])
  stk.append((0,-1,0,0,0))
  while stk:
    cur,par,cur_dep,cur_dis,idx=stk.pop()
    dep[cur]=cur_dep
    dis[cur]=cur_dis
    nxt[0][cur]=par
    if idx!=len(g[cur]):
      stk.append((cur,par,cur_dep,cur_dis,idx+1))
      if g[cur][idx][0]!=par:
        stk.append((g[cur][idx][0],cur,cur_dep+1,cur_dis+g[cur][idx][1],0))
  
  for j in range(logn-1):
    for i in range(n):
      if nxt[j][i]!=-1:
        nxt[j+1][i]=nxt[j][nxt[j][i]]
  def lca(a,b):
    if dep[a]>dep[b]:
      a,b=b,a
    d=dep[b]-dep[a]
    for j in range(logn):
      if d>>j&1:
        b=nxt[j][b]
    if a==b:
      return a
    for j in range(logn-1,-1,-1):
      if nxt[j][a]!=nxt[j][b]:
        a=nxt[j][a]
        b=nxt[j][b]
    return nxt[0][a]
  
  p=[0]*k
  for i in range(k):
    m,p[i]=map(int,input().split())
    xs=list(map(int,input().split()))
    for x in xs:
      g[n+i].append((x-1,0))
      g[x-1].append((n+i,p[i]))
  
  dp=[[1<<60]*(n+k)for _ in range(k)]
  dik=[]
  for i in range(k):
    dpi=dp[i]
    dpi[n+i]=0
    heappush(dik,(0,n+i))
    while dik:
      d,cur=heappop(dik)
      if dpi[cur]<d:
        continue
      for to,cost in g[cur]:
        if dpi[to]>dpi[cur]+cost:
          dpi[to]=dpi[cur]+cost
          dik.append((dpi[to],to))
  
  q=int(input())
  for _ in range(q):
    u,v=map(int,input().split())
    u-=1
    v-=1
    ans=dis[u]+dis[v]-dis[lca(u,v)]*2
    for i in range(k):
      if ans>dp[i][u]+dp[i][v]+p[i]:
        ans=dp[i][u]+dp[i][v]+p[i]
    print(ans)
main()
0