
최소 스패닝 트리를 만드는 데 드는 가중치(비용)를 구하는 문제이다.
크루스칼 알고리즘을 이용하면 쉽게 구할 수 있다.
해결언어 : Python
import sys
input = sys.stdin.readline
v, e = map(int, input().split())
edges = []
for _ in range(e):
a, b, c = map(int, input().split())
edges.append((a, b, c))
edges.sort(key=lambda x:x[2])
def find(x):
if x == uf[x]:
return x
uf[x] = find(uf[x])
return uf[x]
def union(a, b):
x = find(a)
y = find(b)
uf[x] = y
uf = [i for i in range(v+1)]
weight = 0
for a, b, c in edges:
if find(a) != find(b):
union(a, b)
weight += c
print(weight)

끝으로..
크루스칼 알고리즘의 기초를 복습할 수 있는 문제였다.