結果

問題 No.2200 Weird Shortest Path
ユーザー 👑 H20H20
提出日時 2023-01-28 19:35:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,777 ms / 2,000 ms
コード長 1,713 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 133,792 KB
最終ジャッジ日時 2023-09-11 07:36:24
合計ジャッジ時間 44,632 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,436 KB
testcase_01 AC 99 ms
71,752 KB
testcase_02 AC 101 ms
71,612 KB
testcase_03 AC 129 ms
77,556 KB
testcase_04 AC 173 ms
79,428 KB
testcase_05 AC 100 ms
71,552 KB
testcase_06 AC 112 ms
76,380 KB
testcase_07 AC 124 ms
77,540 KB
testcase_08 AC 165 ms
78,792 KB
testcase_09 AC 157 ms
78,936 KB
testcase_10 AC 103 ms
71,520 KB
testcase_11 AC 101 ms
71,520 KB
testcase_12 AC 103 ms
72,316 KB
testcase_13 AC 101 ms
71,584 KB
testcase_14 AC 102 ms
71,520 KB
testcase_15 AC 162 ms
78,952 KB
testcase_16 AC 103 ms
71,700 KB
testcase_17 AC 674 ms
96,352 KB
testcase_18 AC 1,053 ms
108,364 KB
testcase_19 AC 1,594 ms
129,964 KB
testcase_20 AC 1,558 ms
127,164 KB
testcase_21 AC 761 ms
98,892 KB
testcase_22 AC 967 ms
105,520 KB
testcase_23 AC 1,669 ms
129,788 KB
testcase_24 AC 502 ms
87,748 KB
testcase_25 AC 1,555 ms
127,304 KB
testcase_26 AC 1,677 ms
130,872 KB
testcase_27 AC 771 ms
98,196 KB
testcase_28 AC 1,301 ms
117,360 KB
testcase_29 AC 1,687 ms
131,724 KB
testcase_30 AC 1,659 ms
129,912 KB
testcase_31 AC 1,137 ms
111,456 KB
testcase_32 AC 1,749 ms
132,124 KB
testcase_33 AC 1,737 ms
131,760 KB
testcase_34 AC 1,710 ms
131,616 KB
testcase_35 AC 1,777 ms
133,792 KB
testcase_36 AC 1,754 ms
132,684 KB
testcase_37 AC 1,732 ms
132,460 KB
testcase_38 AC 1,728 ms
131,540 KB
testcase_39 AC 997 ms
106,308 KB
testcase_40 AC 1,048 ms
128,884 KB
testcase_41 AC 667 ms
101,840 KB
testcase_42 AC 1,062 ms
125,900 KB
testcase_43 AC 1,614 ms
126,444 KB
testcase_44 AC 1,570 ms
126,220 KB
testcase_45 AC 1,630 ms
127,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict
import heapq

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        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):
        return -self.parents[self.find(x)]

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

    def 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.parents) if x < 0]

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

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())


N,M = map(int, input().split())
ABW = [list(map(int, input().split())) for _ in range(M)]
H = []
for a,b,w in ABW:
    heapq.heappush(H,(w,a,b))
uf = UnionFind(N)
ans = 0
while H:
    w,a,b = heapq.heappop(H)
    a-=1
    b-=1
    if not uf.same(a,b):
        ans += uf.size(a)*uf.size(b)*w
        uf.union(a,b)

print(ans)
0