Prim’s minimum spanning tree · Compiler

Prim’s minimum spanning tree

Trace Prim's algorithm on a connected two-way cable network. Begin with site s. At each step choose a cable with exactly one endpoint already in the tree. Choose the lowest cost, then the smallest outside vertex ID, then the smallest inside vertex ID. Add the outside vertex.

Input
The first line contains n m s. 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. The start s is a vertex ID. Each cable cost w is an integer from 0 to 100.
Output
Print all n site IDs in the order they enter the tree, starting with s, on one space-separated line.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…