結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー Nzt3Nzt3
提出日時 2024-02-11 12:45:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,838 ms / 2,500 ms
コード長 1,001 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 158,712 KB
最終ジャッジ日時 2024-02-11 16:00:11
合計ジャッジ時間 25,096 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 482 ms
95,568 KB
testcase_01 AC 1,028 ms
117,716 KB
testcase_02 AC 1,657 ms
150,608 KB
testcase_03 AC 69 ms
72,792 KB
testcase_04 AC 55 ms
66,504 KB
testcase_05 AC 69 ms
72,780 KB
testcase_06 AC 553 ms
86,724 KB
testcase_07 AC 1,678 ms
156,740 KB
testcase_08 AC 1,499 ms
147,780 KB
testcase_09 AC 1,681 ms
158,020 KB
testcase_10 AC 1,755 ms
158,160 KB
testcase_11 AC 1,838 ms
158,032 KB
testcase_12 AC 1,802 ms
158,032 KB
testcase_13 AC 1,754 ms
158,160 KB
testcase_14 AC 1,771 ms
158,160 KB
testcase_15 AC 378 ms
77,936 KB
testcase_16 AC 397 ms
77,796 KB
testcase_17 AC 397 ms
77,796 KB
testcase_18 AC 812 ms
100,176 KB
testcase_19 AC 1,210 ms
139,724 KB
testcase_20 AC 1,059 ms
128,332 KB
testcase_21 AC 1,336 ms
158,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
from collections import deque
N,M=map(int,stdin.readline().split())
G=[[] for _ in range(N)]
for _ in range(M):
  A,B=map(int,stdin.readline().split())
  A-=1
  B-=1
  G[A].append(B)
  G[B].append(A)

C=list(map(lambda x:int(x)-1,stdin.readline().split()))
W=list(map(int,stdin.readline().split()))

root=[[-1]*N for _ in range(1<<10)]
for i in range(1<<10):
  for j in range(N):
    if root[i][j]!=-1:
      continue
    root[i][j]=j
    if (i>>C[j]&1)==0:
      continue
    q=deque()
    q.append(j)
    while q:
      v=q.popleft()
      for k in G[v]:
        if root[i][k]==-1 and (i>>C[k]&1):
          root[i][k]=j
          q.append(k)

cost=[0]*(1<<10)
for i in range(1<<10):
  for j in range(10):
    if i>>j&1:
      cost[i]+=W[j]

Q=int(stdin.readline())
for _ in range(Q):
  U,V=map(int,stdin.readline().split())
  U-=1
  V-=1
  ans=10**11
  for i in range(1<<10):
    if root[i][U]==root[i][V]:
      ans=min(ans,cost[i])
  if ans==10**11:
    ans=-1
  print(ans)
0