結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー rlangevinrlangevin
提出日時 2024-02-16 21:54:29
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,698 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 245,068 KB
最終ジャッジ日時 2024-02-16 21:54:55
合計ジャッジ時間 21,416 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,171 ms
114,444 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 AC 71 ms
72,740 KB
testcase_04 AC 49 ms
62,084 KB
testcase_05 AC 84 ms
76,344 KB
testcase_06 AC 2,073 ms
97,848 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind():
    def __init__(self, N):
        self._par = [-1] * N
        self._size = [1] * N

    def find(self, x):
        if self._par[x] == x:
            return x
        else:
            vertices = []
            while self._par[x] >= 0:
                vertices.append(x)
                x = self._par[x]
            for i in vertices:
                self._par[i] = x
            return x            

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 
        
        if self._size[x] < self._size[y]:
            x, y = y, x
        self._par[y] = x
        self._size[x] += self._size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self._size[x]

N, M = map(int, input().split())
A, B = [-1] * M, [-1] * M
for i in range(M):
    A[i], B[i] = map(int, input().split())
    A[i], B[i] = A[i] - 1, B[i] - 1
    
C = list(map(int, input().split()))
for i in range(N):
    C[i] -= 1
W = list(map(int, input().split()))
K = 1 << 10
U = [UnionFind(N) for _ in range(K)]
cost = [0] * K
for s in range(K):
    for i in range(M):
        if ((s >> C[A[i]]) & 1) and ((s >> C[B[i]]) & 1):
            U[s].union(A[i], B[i])
    for i in range(10):
        if (s >> i) & 1:
            cost[s] += W[i]
          
Q = int(input())
inf = 10 ** 18  
for _ in range(Q):
    ans = inf
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    for i in range(K):
        if U[i].is_same(u, v):
            ans = min(ans, cost[i])
         
    print(ans) if ans != inf else print(-1)
0