結果

問題 No.2200 Weird Shortest Path
ユーザー titiatitia
提出日時 2023-01-29 00:22:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,256 ms / 2,000 ms
コード長 803 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 62,288 KB
最終ジャッジ日時 2023-09-11 10:26:30
合計ジャッジ時間 29,919 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,904 KB
testcase_01 AC 16 ms
7,848 KB
testcase_02 AC 16 ms
7,960 KB
testcase_03 AC 18 ms
8,220 KB
testcase_04 AC 24 ms
8,776 KB
testcase_05 AC 16 ms
7,848 KB
testcase_06 AC 17 ms
7,812 KB
testcase_07 AC 17 ms
8,256 KB
testcase_08 AC 22 ms
8,536 KB
testcase_09 AC 19 ms
8,372 KB
testcase_10 AC 15 ms
7,812 KB
testcase_11 AC 16 ms
7,812 KB
testcase_12 AC 16 ms
7,948 KB
testcase_13 AC 16 ms
7,888 KB
testcase_14 AC 15 ms
7,944 KB
testcase_15 AC 20 ms
8,376 KB
testcase_16 AC 16 ms
7,804 KB
testcase_17 AC 341 ms
25,000 KB
testcase_18 AC 671 ms
38,676 KB
testcase_19 AC 1,046 ms
58,140 KB
testcase_20 AC 1,064 ms
57,180 KB
testcase_21 AC 465 ms
28,872 KB
testcase_22 AC 564 ms
35,288 KB
testcase_23 AC 1,140 ms
60,288 KB
testcase_24 AC 192 ms
17,524 KB
testcase_25 AC 1,060 ms
57,868 KB
testcase_26 AC 1,106 ms
59,344 KB
testcase_27 AC 414 ms
27,136 KB
testcase_28 AC 817 ms
46,856 KB
testcase_29 AC 1,163 ms
60,136 KB
testcase_30 AC 1,118 ms
59,228 KB
testcase_31 AC 690 ms
40,196 KB
testcase_32 AC 1,256 ms
61,336 KB
testcase_33 AC 1,238 ms
61,332 KB
testcase_34 AC 1,247 ms
61,344 KB
testcase_35 AC 1,222 ms
62,288 KB
testcase_36 AC 1,238 ms
61,476 KB
testcase_37 AC 1,213 ms
61,332 KB
testcase_38 AC 1,230 ms
62,276 KB
testcase_39 AC 675 ms
36,976 KB
testcase_40 AC 975 ms
54,560 KB
testcase_41 AC 635 ms
33,912 KB
testcase_42 AC 944 ms
51,448 KB
testcase_43 AC 1,064 ms
59,840 KB
testcase_44 AC 1,067 ms
59,824 KB
testcase_45 AC 1,072 ms
59,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind

n,m=map(int,input().split())

Group = [i for i in range(n+1)] # グループ分け
Nodes = [1]*(n+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

X=[list(map(int,input().split())) for i in range(m)]
X.sort(key=lambda x:x[2])

ANS=0
for a,b,w in X:
    if find(a)!=find(b):
        x=Nodes[find(a)]
        y=Nodes[find(b)]

        ANS+=w*x*y
        Union(a,b)

print(ANS)
0