結果
| 問題 |
No.2200 Weird Shortest Path
|
| ユーザー |
ntuda
|
| 提出日時 | 2023-02-04 20:35:55 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 729 ms / 2,000 ms |
| コード長 | 619 bytes |
| コンパイル時間 | 414 ms |
| コンパイル使用メモリ | 82,440 KB |
| 実行使用メモリ | 106,976 KB |
| 最終ジャッジ日時 | 2024-07-03 17:03:24 |
| 合計ジャッジ時間 | 20,294 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 44 |
ソースコード
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()
ntuda