結果

問題 No.2200 Weird Shortest Path
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-02-04 19:23:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 969 ms / 2,000 ms
コード長 1,572 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 111,124 KB
最終ジャッジ日時 2023-09-16 15:55:45
合計ジャッジ時間 26,458 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
73,096 KB
testcase_01 AC 96 ms
72,972 KB
testcase_02 AC 103 ms
72,684 KB
testcase_03 AC 106 ms
77,112 KB
testcase_04 AC 118 ms
78,400 KB
testcase_05 AC 99 ms
72,780 KB
testcase_06 AC 101 ms
72,900 KB
testcase_07 AC 105 ms
76,928 KB
testcase_08 AC 117 ms
78,412 KB
testcase_09 AC 115 ms
78,472 KB
testcase_10 AC 99 ms
72,848 KB
testcase_11 AC 99 ms
72,940 KB
testcase_12 AC 101 ms
73,096 KB
testcase_13 AC 98 ms
72,872 KB
testcase_14 AC 100 ms
72,872 KB
testcase_15 AC 116 ms
78,208 KB
testcase_16 AC 98 ms
72,824 KB
testcase_17 AC 417 ms
90,440 KB
testcase_18 AC 595 ms
97,364 KB
testcase_19 AC 881 ms
109,856 KB
testcase_20 AC 873 ms
110,240 KB
testcase_21 AC 456 ms
92,628 KB
testcase_22 AC 572 ms
95,204 KB
testcase_23 AC 916 ms
111,028 KB
testcase_24 AC 327 ms
86,500 KB
testcase_25 AC 876 ms
109,400 KB
testcase_26 AC 929 ms
110,728 KB
testcase_27 AC 482 ms
92,128 KB
testcase_28 AC 740 ms
104,316 KB
testcase_29 AC 921 ms
110,296 KB
testcase_30 AC 935 ms
111,124 KB
testcase_31 AC 647 ms
99,032 KB
testcase_32 AC 934 ms
110,736 KB
testcase_33 AC 937 ms
110,756 KB
testcase_34 AC 956 ms
110,312 KB
testcase_35 AC 948 ms
110,672 KB
testcase_36 AC 950 ms
110,488 KB
testcase_37 AC 969 ms
111,040 KB
testcase_38 AC 954 ms
110,816 KB
testcase_39 AC 609 ms
96,752 KB
testcase_40 AC 490 ms
107,140 KB
testcase_41 AC 389 ms
93,740 KB
testcase_42 AC 482 ms
105,036 KB
testcase_43 AC 931 ms
107,676 KB
testcase_44 AC 935 ms
107,524 KB
testcase_45 AC 925 ms
107,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

N,M = map(int,input().split())
W = []
class DSU:
    def __init__(self, n):
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        x, y = self.leader(a), self.leader(b)
        if x == y: return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self._n
        if self.parent_or_size[a] < 0: return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self._n
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [self.leader(i) for i in range(self._n)]
        result = [[] for _ in range(self._n)]
        for i in range(self._n): result[leader_buf[i]].append(i)
        return [r for r in result if r != []]


for _ in range(M):
    a,b,w = map(int,input().split())
    a -= 1
    b -= 1
    W.append((w,a,b))
ans = 0
W.sort()
D = DSU(N)
for w,a,b in W:
    if D.same(a,b):
        continue
    
    ans += w*D.size(a)*D.size(b)
    D.merge(a,b)
print(ans)
0