結果

問題 No.748 yuki国のお財布事情
ユーザー H3PO4H3PO4
提出日時 2021-01-05 09:18:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,245 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 122,612 KB
最終ジャッジ日時 2024-04-23 15:40:58
合計ジャッジ時間 40,550 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 983 ms
56,560 KB
testcase_01 AC 979 ms
56,440 KB
testcase_02 AC 964 ms
56,572 KB
testcase_03 AC 988 ms
56,688 KB
testcase_04 AC 984 ms
56,568 KB
testcase_05 AC 970 ms
56,576 KB
testcase_06 AC 981 ms
56,568 KB
testcase_07 AC 1,047 ms
56,572 KB
testcase_08 AC 979 ms
56,696 KB
testcase_09 AC 968 ms
56,440 KB
testcase_10 AC 964 ms
56,428 KB
testcase_11 AC 965 ms
56,312 KB
testcase_12 AC 981 ms
56,560 KB
testcase_13 AC 1,070 ms
58,108 KB
testcase_14 AC 1,081 ms
59,764 KB
testcase_15 AC 1,052 ms
58,608 KB
testcase_16 AC 1,192 ms
63,740 KB
testcase_17 AC 1,795 ms
87,256 KB
testcase_18 AC 1,697 ms
80,712 KB
testcase_19 AC 1,698 ms
79,220 KB
testcase_20 AC 1,577 ms
76,284 KB
testcase_21 AC 1,865 ms
92,544 KB
testcase_22 AC 990 ms
56,820 KB
testcase_23 AC 995 ms
56,824 KB
testcase_24 AC 968 ms
56,188 KB
testcase_25 AC 1,664 ms
85,236 KB
testcase_26 TLE -
testcase_27 AC 1,978 ms
108,956 KB
testcase_28 AC 1,464 ms
122,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix

input = sys.stdin.buffer.readline
MAX = 10 ** 10


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

    def issame(self, x, y):
        return self.find(x) == self.find(y)

    def group_size(self, x):
        return self.size[self.find(x)]

    def group_members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parent) if i == x]

    def group_count(self):
        return len(self.roots())


N, M, K = map(int, input().split())
ABC = []
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    ABC.append([a, b, c])

cost = 0

uf = UnionFind(N)
for _ in range(K):
    e = int(input()) - 1
    a, b, c = ABC[e]
    uf.unite(a, b)
    cost += c

compress_dict = {x: i for i, x in enumerate(uf.roots())}
compress_N = uf.group_count()
d = defaultdict(lambda: MAX)
for a, b, c in ABC:
    a, b = uf.find(a), uf.find(b)
    if a == b:
        continue
    a, b = compress_dict[a], compress_dict[b]
    if a > b: a, b = b, a
    d[(a, b)] = min(d[(a, b)], c)

length, frm, to = [], [], []
for (a, b), c in d.items():
    length.append(c)
    frm.append(a)
    to.append(b)
matr = csr_matrix((length, (frm, to)), shape=(compress_N, compress_N))
T = minimum_spanning_tree(matr)
cost += int(T.sum())
ans = sum(c for _, _, c in ABC) - cost
print(ans)
0