結果
| 問題 | No.2630 Colorful Vertices and Cheapest Paths | 
| コンテスト | |
| ユーザー |  ikoma | 
| 提出日時 | 2024-02-16 22:48:45 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2,287 ms / 2,500 ms | 
| コード長 | 1,385 bytes | 
| コンパイル時間 | 285 ms | 
| コンパイル使用メモリ | 82,404 KB | 
| 実行使用メモリ | 162,584 KB | 
| 最終ジャッジ日時 | 2024-09-28 21:25:03 | 
| 合計ジャッジ時間 | 27,646 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 22 | 
ソースコード
import sys
input = sys.stdin.readline
class UnionFind:
    def __init__(self, n:int):
        self.n = n
        self.parents = [-1] * n
    def find(self, x:int):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x:int, y:int):
        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:int):
        return -self.parents[self.find(x)]
    def same(self, x:int, y:int):
        return self.find(x) == self.find(y)
N,M=map(int,input().split())
AB=[list(map(lambda x:int(x)-1,input().split())) for _ in range(M)]
C=list(map(lambda x:int(x)-1,input().split()))
W=list(map(int,input().split()))
bit_uf = []
for bit in range(1<<10):
    uf = UnionFind(N)
    for a,b in AB:
        if ((bit>>C[a])&1) and ((bit>>C[b])&1):
            uf.union(a,b)
    cost = 0
    for i in range(10):
        if (bit>>i)&1:
            cost += W[i]
    bit_uf.append((uf,cost))
Q=int(input())
for _ in range(Q):
    u,v=map(lambda x:int(x)-1,input().split())
    ans = 1<<60
    for uf,c in bit_uf:
        if uf.same(u,v):
            ans = min(ans, c)
    print(ans if ans < (1<<60) else -1)
            
            
            
        