結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー miya145592miya145592
提出日時 2024-02-16 22:27:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,362 ms / 2,500 ms
コード長 2,138 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 102,816 KB
最終ジャッジ日時 2024-02-16 22:27:31
合計ジャッジ時間 19,600 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 697 ms
82,392 KB
testcase_01 AC 1,140 ms
88,468 KB
testcase_02 AC 1,082 ms
96,324 KB
testcase_03 AC 88 ms
76,120 KB
testcase_04 AC 50 ms
64,584 KB
testcase_05 AC 84 ms
75,964 KB
testcase_06 AC 412 ms
79,168 KB
testcase_07 AC 757 ms
94,660 KB
testcase_08 AC 732 ms
91,420 KB
testcase_09 AC 774 ms
95,044 KB
testcase_10 AC 1,362 ms
101,508 KB
testcase_11 AC 1,327 ms
102,780 KB
testcase_12 AC 1,347 ms
102,816 KB
testcase_13 AC 1,344 ms
102,416 KB
testcase_14 AC 1,329 ms
101,188 KB
testcase_15 AC 843 ms
81,540 KB
testcase_16 AC 796 ms
81,572 KB
testcase_17 AC 828 ms
81,752 KB
testcase_18 AC 459 ms
80,736 KB
testcase_19 AC 652 ms
89,412 KB
testcase_20 AC 534 ms
85,444 KB
testcase_21 AC 753 ms
96,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n, w=None):
        self.par = [-1]*n
        self.rank = [0]*n
        self.siz = [1]*n
        self.cnt = n
        self.min_node = [i for i in range(n)]
        self.weight = w if w else [1]*n

    def root(self, x):
        if self.par[x] == -1:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def issame(self, x, y):
        return self.root(x) == self.root(y)
            
    def unite(self, x, y):
        px = self.root(x)
        py = self.root(y)
        if px == py:
            return False
        if self.rank[px] < self.rank[py]:
            px, py = py, px
        self.par[py] = px
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        self.siz[px] += self.siz[py]
        self.cnt -= 1
        self.min_node[px] = min(self.min_node[px], self.min_node[py])
        self.weight[px] += self.weight[py]
        return False

    def count(self):
        return self.cnt

    def min(self, x):
        return self.min_node[self.root(x)]

    def getweight(self, x):
        return self.weight[self.root(x)]
    
    def size(self, x):
        return self.siz[self.root(x)]

import sys
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in range(N)]
AB = [list(map(int, input().split())) for _ in range(M)]
C = list(map(int, input().split()))
W = list(map(int, input().split()))
Q = int(input())
UV = [list(map(int, input().split())) for _ in range(Q)]
for i in range(M):
    a, b = AB[i]
    a-=1
    b-=1
    AB[i] = [a, b]
for i in range(N):
    C[i]-=1
for i in range(Q):
    u, v = UV[i]
    u-=1
    v-=1
    UV[i] = [u, v]
INF = 10**12
ans = [INF for _ in range(Q)]
for i in range(1, 1<<10):
    UF = UnionFind(N)
    for a, b in AB:
        if (i>>C[a])&1 and (i>>C[b])&1:
            UF.unite(a, b)
    cost = 0
    for j in range(10):
        if (i>>j)&1:
            cost += W[j]
    for j in range(Q):
        u, v = UV[j]
        if UF.issame(u, v):
            ans[j] = min(ans[j], cost)
for a in ans:
    if a==INF:
        print(-1)
    else:
        print(a)
        
0