Kruskal’s minimum spanning tree · Compiler
Kruskal’s minimum spanning tree
Use Kruskal's algorithm to connect all sites with cables of minimum total cost. Each available cable joins two sites in both directions. The network of available cables is connected.
- Input
- The first line contains n m. Vertices are numbered 0 through n-1 (1 <= n <= 50, 0 <= m <= 200). Each of the next m lines contains u v w for an edge joining u and v in both directions. Endpoints differ and each unordered pair occurs at most once. Each cable cost w is an integer from 0 to 100.
- Output
- Print the total cost of a minimum spanning tree. Trees tied for minimum cost give the same total. A single site costs 0 to connect.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…