Skip to content

Commit

Permalink
2024-7-14 전력난
Browse files Browse the repository at this point in the history
  • Loading branch information
seongwon030 committed Jul 14, 2024
1 parent 6dcfbbe commit 051e59c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions seongwon030/최소스패닝트리/전력난.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
input = sys.stdin.readline

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


while True:
V,E = map(int,input().split())
if V==0 and E==0:
break
root = [i for i in range(V+1)]
edge = [] # 간선리스트
for i in range(E):
a,b,c = map(int,input().split())
edge.append((a,b,c))

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

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

print(ans)

0 comments on commit 051e59c

Please sign in to comment.