結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 65 ms
73,440 KB
testcase_04 AC 47 ms
62,384 KB
testcase_05 AC 63 ms
73,436 KB
testcase_06 AC 657 ms
85,656 KB
testcase_07 AC 1,351 ms
157,416 KB
testcase_08 AC 1,329 ms
148,456 KB
testcase_09 AC 1,350 ms
159,080 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 718 ms
79,020 KB
testcase_16 AC 645 ms
79,036 KB
testcase_17 AC 699 ms
78,892 KB
testcase_18 AC 1,150 ms
99,992 KB
testcase_19 AC 1,367 ms
139,368 KB
testcase_20 AC 1,287 ms
127,976 KB
testcase_21 AC 1,418 ms
158,696 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)
  DD[now]=(par,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