結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 timitimi
提出日時 2024-02-17 17:09:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,070 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 169,484 KB
最終ジャッジ日時 2024-02-17 17:10:05
合計ジャッジ時間 27,669 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 69 ms
74,580 KB
testcase_04 AC 46 ms
64,576 KB
testcase_05 AC 67 ms
74,444 KB
testcase_06 AC 636 ms
87,160 KB
testcase_07 AC 1,550 ms
167,176 KB
testcase_08 AC 1,406 ms
157,028 KB
testcase_09 AC 1,515 ms
167,320 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 677 ms
79,504 KB
testcase_16 AC 696 ms
79,240 KB
testcase_17 AC 716 ms
79,552 KB
testcase_18 AC 992 ms
100,260 KB
testcase_19 AC 1,480 ms
139,368 KB
testcase_20 AC 1,403 ms
131,816 KB
testcase_21 AC 1,577 ms
167,028 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={}
par=[i for i in range(N)];rank=[0]*(N)  
for j in range(2**10):
  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))
    par[i]=i;rank[i]=0
  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