結果
問題 |
No.2200 Weird Shortest Path
|
ユーザー |
![]() |
提出日時 | 2025-03-20 18:40:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 893 ms / 2,000 ms |
コード長 | 980 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,340 KB |
実行使用メモリ | 141,404 KB |
最終ジャッジ日時 | 2025-03-20 18:41:13 |
合計ジャッジ時間 | 22,587 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 44 |
ソースコード
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()