結果
| 問題 |
No.2630 Colorful Vertices and Cheapest Paths
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-16 21:38:27 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,112 ms / 2,500 ms |
| コード長 | 2,390 bytes |
| コンパイル時間 | 233 ms |
| コンパイル使用メモリ | 82,712 KB |
| 実行使用メモリ | 96,524 KB |
| 最終ジャッジ日時 | 2024-09-28 19:52:59 |
| 合計ジャッジ時間 | 17,682 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
input = sys.stdin.readline
#n,m = map(int,input().split())
#for i in range(n):
# alist.append(list(map(int,input().split())))
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
group_members = collections.defaultdict(list)
for member in range(self.n):
group_members[self.find(member)].append(member)
return group_members
def __str__(self):
return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items())
n,m = map(int,input().split())
edge = [[] for i in range(n)]
e = []
for i in range(m):
u,v = map(int,input().split())
u-=1
v-=1
e.append((u,v))
edge[u].append(v)
edge[v].append(u)
color = list(map(lambda x:int(x)-1,input().split()))
w = list(map(int,input().split()))
q = int(input())
query = [list(map(lambda x:int(x)-1,input().split())) for i in range(q)]
ans = [10**18 for i in range(q)]
for i in itertools.product([0,1],repeat=10):
uf = UnionFind(n)
cost = sum(w[j] if i[j] == 1 else 0 for j in range(10))
for u,v in e:
if i[color[u]] == 1 and i[color[v]] == 1:
uf.union(u,v)
for j in range(q):
x,y = query[j]
if i[color[x]] == 1 and i[color[y]] == 1 and uf.same(x,y):
ans[j] = min(ans[j],cost)
for i in ans:
print(-1 if i == 10**18 else i)