結果

問題 No.2200 Weird Shortest Path
ユーザー ntudantuda
提出日時 2023-02-04 20:35:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 765 ms / 2,000 ms
コード長 619 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 108,096 KB
最終ジャッジ日時 2023-09-16 17:08:29
合計ジャッジ時間 23,086 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,012 KB
testcase_01 AC 70 ms
71,136 KB
testcase_02 AC 74 ms
71,412 KB
testcase_03 AC 84 ms
75,684 KB
testcase_04 AC 98 ms
76,684 KB
testcase_05 AC 70 ms
71,072 KB
testcase_06 AC 74 ms
71,372 KB
testcase_07 AC 82 ms
75,680 KB
testcase_08 AC 99 ms
76,820 KB
testcase_09 AC 99 ms
76,624 KB
testcase_10 AC 72 ms
71,032 KB
testcase_11 AC 71 ms
71,500 KB
testcase_12 AC 74 ms
71,528 KB
testcase_13 AC 72 ms
71,276 KB
testcase_14 AC 72 ms
71,456 KB
testcase_15 AC 98 ms
76,836 KB
testcase_16 AC 72 ms
71,264 KB
testcase_17 AC 367 ms
86,064 KB
testcase_18 AC 530 ms
92,508 KB
testcase_19 AC 681 ms
101,164 KB
testcase_20 AC 719 ms
101,708 KB
testcase_21 AC 396 ms
88,452 KB
testcase_22 AC 477 ms
92,244 KB
testcase_23 AC 730 ms
102,640 KB
testcase_24 AC 288 ms
83,944 KB
testcase_25 AC 759 ms
108,096 KB
testcase_26 AC 726 ms
102,016 KB
testcase_27 AC 409 ms
89,228 KB
testcase_28 AC 609 ms
96,976 KB
testcase_29 AC 750 ms
102,996 KB
testcase_30 AC 719 ms
101,140 KB
testcase_31 AC 513 ms
93,596 KB
testcase_32 AC 745 ms
102,804 KB
testcase_33 AC 742 ms
102,512 KB
testcase_34 AC 753 ms
102,356 KB
testcase_35 AC 763 ms
103,172 KB
testcase_36 AC 754 ms
102,360 KB
testcase_37 AC 765 ms
102,808 KB
testcase_38 AC 763 ms
102,124 KB
testcase_39 AC 400 ms
91,412 KB
testcase_40 AC 333 ms
103,444 KB
testcase_41 AC 297 ms
88,540 KB
testcase_42 AC 328 ms
104,956 KB
testcase_43 AC 520 ms
103,364 KB
testcase_44 AC 520 ms
103,792 KB
testcase_45 AC 493 ms
103,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
ABW = [list(map(int, input().split())) for _ in range(M)]

def root(x):
    if P[x] < 0: return x
    P[x] = root(P[x])  # 経路圧縮
    return P[x]

def unite(x, y):
    x = root(x)
    y = root(y)
    if x == y: return
    if x > y: x, y = y, x
    P[x] += P[y]
    P[y] = x

def same(x, y):
    return root(x) == root(y)

def size(x):
    x = root(x)
    return -P[x]

P = [-1] * (N + 1)
ABW.sort(key=lambda x: x[2])
ans = 0
for a, b, w in ABW:
    if not same(a, b):
        ans += w * size(a) * size(b)
        unite(a, b)
    if size(1) == N:
        print(ans)
        exit()
0