結果

問題 No.2200 Weird Shortest Path
ユーザー lam6er
提出日時 2025-03-20 20:18:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 946 ms / 2,000 ms
コード長 980 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,664 KB
実行使用メモリ 141,520 KB
最終ジャッジ日時 2025-03-20 20:19:26
合計ジャッジ時間 23,659 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx +=1
    M = int(input[idx])
    idx +=1
    edges = []
    for _ in range(M):
        a = int(input[idx])-1
        idx +=1
        b = int(input[idx])-1
        idx +=1
        w = int(input[idx])
        idx +=1
        edges.append((w, a, b))
    edges.sort()
    
    parent = list(range(N))
    size = [1]*N
    
    def find(u):
        while parent[u] != u:
            parent[u] = parent[parent[u]]
            u = parent[u]
        return u
    
    sum_total = 0
    for w, a, b in edges:
        root_a = find(a)
        root_b = find(b)
        if root_a != root_b:
            sum_total += w * size[root_a] * size[root_b]
            if size[root_a] < size[root_b]:
                root_a, root_b = root_b, root_a
            parent[root_b] = root_a
            size[root_a] += size[root_b]
    
    print(sum_total)
    
if __name__ == "__main__":
    main()
0