結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー rlangevinrlangevin
提出日時 2024-02-16 22:01:01
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,702 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 161,788 KB
最終ジャッジ日時 2024-02-16 22:01:43
合計ジャッジ時間 29,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 751 ms
99,092 KB
testcase_01 AC 1,505 ms
120,952 KB
testcase_02 AC 1,724 ms
153,424 KB
testcase_03 AC 94 ms
78,276 KB
testcase_04 AC 68 ms
73,168 KB
testcase_05 AC 101 ms
78,152 KB
testcase_06 AC 682 ms
88,652 KB
testcase_07 AC 1,444 ms
158,536 KB
testcase_08 AC 1,410 ms
149,320 KB
testcase_09 AC 1,626 ms
159,944 KB
testcase_10 TLE -
testcase_11 AC 2,128 ms
161,264 KB
testcase_12 AC 2,085 ms
161,392 KB
testcase_13 AC 2,112 ms
161,244 KB
testcase_14 AC 2,061 ms
161,680 KB
testcase_15 AC 642 ms
80,332 KB
testcase_16 AC 645 ms
80,492 KB
testcase_17 AC 625 ms
80,332 KB
testcase_18 AC 921 ms
101,832 KB
testcase_19 AC 1,191 ms
141,000 KB
testcase_20 AC 1,115 ms
129,224 KB
testcase_21 AC 1,304 ms
159,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
#https://github.com/not522/ac-library-python/blob/master/atcoder/dsu.py
import typing


class DSU:
    '''
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))

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 = [DSU(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].merge(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].same(u, v):
            ans = min(ans, cost[i])
         
    print(ans) if ans != inf else print(-1)
0