結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー amesyuamesyu
提出日時 2024-02-16 22:13:03
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,681 bytes
コンパイル時間 483 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 328,920 KB
最終ジャッジ日時 2024-02-16 22:13:40
合計ジャッジ時間 21,279 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,073 ms
142,660 KB
testcase_01 AC 2,098 ms
201,768 KB
testcase_02 TLE -
testcase_03 AC 87 ms
76,088 KB
testcase_04 AC 56 ms
66,372 KB
testcase_05 AC 92 ms
76,352 KB
testcase_06 AC 1,280 ms
106,376 KB
testcase_07 AC 2,330 ms
315,248 KB
testcase_08 AC 2,258 ms
288,624 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:

    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.size = [0] * n
        self.diff = [0] * n

    def find(self, x):
        if self.parent[x] == x:
            return x

        root = self.find(self.parent[x])
        self.diff[x] += self.diff[self.parent[x]]
        self.parent[x] = root
        return root

    def unite(self, a, b, weight=0):
        rx = self.find(a)
        ry = self.find(b)

        if self.size[rx] < self.size[ry]:
            self.parent[rx] = ry
            self.diff[rx] = weight - self.diff[a] + self.diff[b]
        else:
            self.parent[ry] = rx
            self.diff[ry] = - weight - self.diff[b] + self.diff[a]
            if self.size[rx] == self.size[ry]:
                self.size[rx] += 1

    def same(self, a, b):
        return self.find(a) == self.find(b)

    def cost(self, a, b):
        return self.diff[a] - self.diff[b]

inf = int(1e18) + 2525
n, m = map(int, input().split())
edge = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
c = list(map(int, input().split()))
w = list(map(int, input().split()))

ufs = [UnionFind(n) for _ in range(2**10)]
ufc = [0] * 2**10

for bit in range(2**10):
    ok = [bit & (1<<i) for i in range(10)]
    for i in range(10):
        if ok[i]: ufc[bit] += w[i]

    for u, v in edge:
        if ok[c[u]-1] and ok[c[v]-1]:
            ufs[bit].unite(u, v)

q = int(input())
for _ in range(q):
    u, v = map(lambda x: int(x) - 1, input().split())
    ans = inf
    for bit in range(2**10):
        if ufs[bit].same(u, v):
            ans = min(ans, ufc[bit])

    print(-1 if ans == inf else ans)
0