-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dcfbbe
commit 051e59c
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |