結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー rlangevinrlangevin
提出日時 2024-02-16 21:47:43
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,678 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 320,872 KB
最終ジャッジ日時 2024-02-16 21:48:17
合計ジャッジ時間 31,276 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 771 ms
135,180 KB
testcase_01 AC 1,468 ms
200,752 KB
testcase_02 AC 2,011 ms
297,012 KB
testcase_03 AC 80 ms
76,032 KB
testcase_04 AC 54 ms
66,340 KB
testcase_05 AC 86 ms
76,156 KB
testcase_06 AC 721 ms
106,324 KB
testcase_07 AC 1,712 ms
313,804 KB
testcase_08 AC 1,639 ms
287,692 KB
testcase_09 AC 1,941 ms
317,516 KB
testcase_10 TLE -
testcase_11 AC 2,230 ms
320,744 KB
testcase_12 AC 2,265 ms
320,624 KB
testcase_13 TLE -
testcase_14 AC 2,253 ms
320,528 KB
testcase_15 AC 633 ms
82,084 KB
testcase_16 AC 637 ms
81,432 KB
testcase_17 AC 648 ms
82,104 KB
testcase_18 AC 1,002 ms
145,740 KB
testcase_19 AC 1,255 ms
261,580 KB
testcase_20 AC 1,189 ms
227,660 KB
testcase_21 AC 1,485 ms
317,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind():
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            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