c++ competitive programming mst

To implement Prim's algorithm for finding the Minimum Spanning Tree (MST) in C++, follow these steps:

  1. Start with an arbitrary node as the initial vertex.
  2. Initialize a boolean array to track visited vertices and a vector to store the MST.
  3. Set the initial vertex distance to 0 and push it into a priority queue.
  4. While the priority queue is not empty, do the following:
  5. Extract the vertex with the minimum distance from the priority queue.
  6. Mark the vertex as visited.
  7. Iterate through its adjacent vertices:
    • If the adjacent vertex is not visited and its edge weight is less than the current distance, update the distance and push it into the priority queue.
  8. Push the minimum distance vertex into the MST vector.
  9. The MST vector now contains the minimum spanning tree.