結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 timitimi
提出日時 2024-02-17 17:07:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,932 ms / 2,500 ms
コード長 1,052 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 176,628 KB
最終ジャッジ日時 2024-02-17 17:07:51
合計ジャッジ時間 28,539 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 625 ms
97,656 KB
testcase_01 AC 1,335 ms
124,912 KB
testcase_02 AC 1,690 ms
164,584 KB
testcase_03 AC 87 ms
76,352 KB
testcase_04 AC 48 ms
66,656 KB
testcase_05 AC 71 ms
76,596 KB
testcase_06 AC 590 ms
87,284 KB
testcase_07 AC 1,560 ms
173,160 KB
testcase_08 AC 1,490 ms
162,408 KB
testcase_09 AC 1,531 ms
173,800 KB
testcase_10 AC 1,914 ms
176,344 KB
testcase_11 AC 1,904 ms
176,628 KB
testcase_12 AC 1,913 ms
176,232 KB
testcase_13 AC 1,893 ms
176,256 KB
testcase_14 AC 1,932 ms
176,312 KB
testcase_15 AC 667 ms
80,076 KB
testcase_16 AC 707 ms
79,464 KB
testcase_17 AC 674 ms
79,840 KB
testcase_18 AC 988 ms
100,912 KB
testcase_19 AC 1,589 ms
143,720 KB
testcase_20 AC 1,372 ms
135,656 KB
testcase_21 AC 1,598 ms
174,180 KB
権限があれば一括ダウンロードができます

ソースコード

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
DD={}
for j in range(2**10):
  par=[i for i in range(N)]
  rank=[0]*(N)   
  D=[];s=0;now=j
  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)
  E=[]
  for i in range(N):
    E.append(find(i))
  DD[now]=(E,s)

Q=int(input())
for _ in range(Q):
  ans=10**20
  a,b=map(int, input().split())
  for d in DD:
    P,s=DD[d]
    if P[a-1]==P[b-1]:
      ans=min(ans,s)
  if ans==10**20:
    print(-1)
  else:
    print(ans)
0