Skip to content

Commit

Permalink
2024-07-05 최소스패닝트리
Browse files Browse the repository at this point in the history
  • Loading branch information
seongwon030 committed Jul 5, 2024
1 parent 5600fa0 commit 0d3af7f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions seongwon030/최소스패닝트리/최소스패닝트리.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys

input = sys.stdin.readline

V,E = map(int,input().split())
root = [i for i in range(V+1)]
edge = [] # 간선리스트
for i in range(E):
edge.append(list(map(int,input().split())))

# 비용을 기준으로 오름차순
edge.sort(key=lambda x:x[2])

def find(x):
if x!=root[x]:
root[x] = find(root[x])
return root[x]

ans = 0
for a,b,c in edge:
aRoot = find(a)
bRoot = find(b)
if aRoot != bRoot:
if aRoot > bRoot:
root[aRoot] = bRoot
else:
root[bRoot] = aRoot
ans += c

print(ans)

0 comments on commit 0d3af7f

Please sign in to comment.