結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー first_vilfirst_vil
提出日時 2021-02-01 19:26:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,549 ms / 3,000 ms
コード長 1,846 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 82,724 KB
実行使用メモリ 223,232 KB
最終ジャッジ日時 2024-04-19 20:13:36
合計ジャッジ時間 19,190 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,376 KB
testcase_01 AC 41 ms
53,248 KB
testcase_02 AC 164 ms
80,208 KB
testcase_03 AC 236 ms
78,928 KB
testcase_04 AC 154 ms
79,412 KB
testcase_05 AC 140 ms
79,356 KB
testcase_06 AC 239 ms
79,232 KB
testcase_07 AC 141 ms
78,752 KB
testcase_08 AC 210 ms
78,592 KB
testcase_09 AC 178 ms
80,676 KB
testcase_10 AC 259 ms
79,980 KB
testcase_11 AC 257 ms
80,088 KB
testcase_12 AC 1,023 ms
124,928 KB
testcase_13 AC 411 ms
108,896 KB
testcase_14 AC 732 ms
121,600 KB
testcase_15 AC 635 ms
111,872 KB
testcase_16 AC 928 ms
115,496 KB
testcase_17 AC 1,520 ms
123,268 KB
testcase_18 AC 1,496 ms
124,820 KB
testcase_19 AC 1,115 ms
123,648 KB
testcase_20 AC 1,538 ms
123,136 KB
testcase_21 AC 1,549 ms
124,244 KB
testcase_22 AC 528 ms
204,672 KB
testcase_23 AC 1,275 ms
223,232 KB
testcase_24 AC 432 ms
109,952 KB
testcase_25 AC 981 ms
130,432 KB
testcase_26 AC 436 ms
123,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
  import sys
  input=sys.stdin.buffer.readline
  sys.setrecursionlimit(10**7)
  
  from heapq import heappush,heappop
  
  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)]
  def dfs(cur,par,cur_dep,cur_dis):
    dep[cur]=cur_dep
    dis[cur]=cur_dis
    nxt[0][cur]=par
    for to,cost in g[cur]:
      if to!=par:
        dfs(to,cur,cur_dep+1,cur_dis+cost)
  dfs(0,-1,0,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]
  def dist(a,b):
    return dis[a]+dis[b]-dis[lca(a,b)]*2
  
  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=[]
  border=100100
  for i in range(k):
    dp[i][n+i]=0
    heappush(dik,n+i)
    while dik:
      d=heappop(dik)
      cur=d%border
      d//=border
      if dp[i][cur]<d:
        continue
      for to,cost in g[cur]:
        if dp[i][to]>dp[i][cur]+cost:
          dp[i][to]=dp[i][cur]+cost
          dik.append(dp[i][to]*border+to)
  
  q=int(input())
  for _ in range(q):
    u,v=map(int,input().split())
    u-=1
    v-=1
    ans=dist(u,v)
    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