結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ikomaikoma
提出日時 2024-02-16 22:48:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,452 ms / 2,500 ms
コード長 1,385 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 161,216 KB
最終ジャッジ日時 2024-02-16 22:49:16
合計ジャッジ時間 29,500 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 714 ms
97,916 KB
testcase_01 AC 1,424 ms
120,508 KB
testcase_02 AC 1,905 ms
152,340 KB
testcase_03 AC 63 ms
70,668 KB
testcase_04 AC 44 ms
62,248 KB
testcase_05 AC 71 ms
73,136 KB
testcase_06 AC 646 ms
86,460 KB
testcase_07 AC 1,621 ms
156,992 KB
testcase_08 AC 1,504 ms
148,160 KB
testcase_09 AC 1,768 ms
158,272 KB
testcase_10 AC 2,452 ms
161,164 KB
testcase_11 AC 2,064 ms
160,940 KB
testcase_12 AC 2,165 ms
161,216 KB
testcase_13 AC 2,088 ms
160,676 KB
testcase_14 AC 2,170 ms
161,072 KB
testcase_15 AC 663 ms
79,716 KB
testcase_16 AC 635 ms
79,712 KB
testcase_17 AC 664 ms
79,712 KB
testcase_18 AC 1,028 ms
96,300 KB
testcase_19 AC 1,265 ms
136,128 KB
testcase_20 AC 1,207 ms
127,808 KB
testcase_21 AC 1,416 ms
152,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0