여러 개의 섬 사이에 다리를 놓아야 합니다.
모든 섬이 서로 통행 가능하도록 만들되, 다리 건설 비용의 총합이 최소가 되도록 해야 합니다.
예를 들어,
n = 4,
costs = [[0,1,1],[0,2,2],[1,2,5],[1,3,1],[2,3,8]]
이 경우 최소 비용은 4 (0-1, 0-2, 1-3 연결)

n: 1 이상 100 이하costs[i] = [a, b, cost]: 섬 a와 b를 cost의 비용으로 연결 가능costs)를 비용 순으로 정렬connected)을 만듦 import Foundation
func solution(_ n:Int, _ costs:[[Int]]) -> Int {
var total = 0
var connected: Set<Int> = [costs[0][0]] // 시작 섬 하나만 포함
let sorted = costs.sorted { $0[2] < $1[2] } // 비용 오름차순 정렬
while connected.count < n {
for node in sorted {
let start = node[0]
let end = node[1]
let cost = node[2]
// 하나는 연결되어 있고, 다른 하나는 아직 미연결일 때
if connected.contains(start) && !connected.contains(end) {
connected.insert(end)
total += cost
break
}
if connected.contains(end) && !connected.contains(start) {
connected.insert(start)
total += cost
break
}
}
}
return total
}