結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 timitimi
提出日時 2024-02-17 16:11:07
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 968 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 97,396 KB
最終ジャッジ日時 2024-02-17 16:11:16
合計ジャッジ時間 8,429 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int, input().split())
A=[]
for i in range(M):
  a,b=map(int, input().split())
  a-=1;b-=1
  A.append((a,b))
  
C=list(map(int, input().split()))
W=list(map(int, input().split()))
par=[i for i in range(N)]
rank=[0]*(N)
def find(x):
  if par[x]==x:
    return x
  else:
    par[x]=find(par[x])
    return par[x]
 
#同じ集合か判定
def same(x,y):
  return find(x)==find(y)
 
def union(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return 
  if rank[x]>rank[y]:
    par[y]=x
  else:
    par[x]=y
    if rank[x]==rank[y]:
      rank[y]+=1

Q=int(input())
for _ in range(Q):
  ans=10**20
  a,b=map(int, input().split())
  for j in range(2**10):
    par=[i for i in range(N)]
    rank=[0]*(N)
    D=[];s=0
    for k in range(10):
      D.append(j%2)
      s+=D[-1]*W[k]
      j//=2 
    for x,y in A:
      if D[C[x]-1]==1 and D[C[y]-1]==1:
        union(x,y)
    if same(a-1,b-1):
      ans=min(ans,s)
  if ans==10**20:
    print(-1)
  else:
    print(ans)
    
0