fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <climits>
  5. using namespace std;
  6.  
  7. // Structure to represent a weighted edge in the graph
  8. struct Edge {
  9. int dest;
  10. int weight;
  11. };
  12.  
  13. // Structure to represent a vertex in the priority queue
  14. struct Vertex {
  15. int id;
  16. int key;
  17.  
  18. // Overload the < operator for priority queue
  19. bool operator<(const Vertex& other) const {
  20. return key > other.key; // For min-heap
  21. }
  22. };
  23.  
  24. // Function to implement Prim's algorithm
  25. void primMST(vector<vector<Edge>>& graph, int V) {
  26. // Create a priority queue to store vertices
  27. priority_queue<Vertex> pq;
  28.  
  29. // Create a vector to store keys of all vertices
  30. vector<int> key(V, INT_MAX);
  31.  
  32. // Create a vector to store the MST
  33. vector<int> parent(V, -1);
  34.  
  35. // Create a vector to track vertices included in MST
  36. vector<bool> inMST(V, false);
  37.  
  38. // Start with the first vertex
  39. int src = 0;
  40. key[src] = 0;
  41. pq.push({src, key[src]});
  42.  
  43. // Loop until priority queue is empty
  44. while (!pq.empty()) {
  45. // Extract the minimum key vertex
  46. int u = pq.top().id;
  47. pq.pop();
  48.  
  49. // If already in MST, skip
  50. if (inMST[u]) continue;
  51.  
  52. // Include vertex in MST
  53. inMST[u] = true;
  54.  
  55. // Process all adjacent vertices
  56. for (const Edge& edge : graph[u]) {
  57. int v = edge.dest;
  58. int weight = edge.weight;
  59.  
  60. // If v is not in MST and weight is smaller than current key
  61. if (!inMST[v] && weight < key[v]) {
  62. // Update key and parent
  63. key[v] = weight;
  64. parent[v] = u;
  65. pq.push({v, key[v]});
  66. }
  67. }
  68. }
  69.  
  70. // Print the constructed MST
  71. cout << "Edge \tWeight" << endl;
  72. int totalWeight = 0;
  73. for (int i = 1; i < V; i++) {
  74. cout << parent[i] << " - " << i << "\t" << key[i] << endl;
  75. totalWeight += key[i];
  76. }
  77. cout << "Total Weight of MST: " << totalWeight << endl;
  78. }
  79.  
  80. int main() {
  81. int V, E;
  82. cout << "Enter number of vertices: ";
  83. cin >> V;
  84. cout << "Enter number of edges: ";
  85. cin >> E;
  86.  
  87. // Create an adjacency list representation of the graph
  88. vector<vector<Edge>> graph(V);
  89.  
  90. cout << "Enter edges (source destination weight):" << endl;
  91. for (int i = 0; i < E; i++) {
  92. int src, dest, weight;
  93. cin >> src >> dest >> weight;
  94.  
  95. // Add edge to graph (undirected graph, so add both ways)
  96. graph[src].push_back({dest, weight});
  97. graph[dest].push_back({src, weight});
  98. }
  99.  
  100. // Find MST using Prim's algorithm
  101. primMST(graph, V);
  102.  
  103. return 0;
  104. }
Success #stdin #stdout 0.03s 25316KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

// Structure to represent a weighted edge in the graph
struct Edge {
    int dest;
    int weight;
};

// Structure to represent a vertex in the priority queue
struct Vertex {
    int id;
    int key;
    
    // Overload the < operator for priority queue
    bool operator<(const Vertex& other) const {
        return key > other.key;  // For min-heap
    }
};

// Function to implement Prim's algorithm
void primMST(vector<vector<Edge>>& graph, int V) {
    // Create a priority queue to store vertices
    priority_queue<Vertex> pq;
    
    // Create a vector to store keys of all vertices
    vector<int> key(V, INT_MAX);
    
    // Create a vector to store the MST
    vector<int> parent(V, -1);
    
    // Create a vector to track vertices included in MST
    vector<bool> inMST(V, false);
    
    // Start with the first vertex
    int src = 0;
    key[src] = 0;
    pq.push({src, key[src]});
    
    // Loop until priority queue is empty
    while (!pq.empty()) {
        // Extract the minimum key vertex
        int u = pq.top().id;
        pq.pop();
        
        // If already in MST, skip
        if (inMST[u]) continue;
        
        // Include vertex in MST
        inMST[u] = true;
        
        // Process all adjacent vertices
        for (const Edge& edge : graph[u]) {
            int v = edge.dest;
            int weight = edge.weight;
            
            // If v is not in MST and weight is smaller than current key
            if (!inMST[v] && weight < key[v]) {
                // Update key and parent
                key[v] = weight;
                parent[v] = u;
                pq.push({v, key[v]});
            }
        }
    }
    
    // Print the constructed MST
    cout << "Edge \tWeight" << endl;
    int totalWeight = 0;
    for (int i = 1; i < V; i++) {
        cout << parent[i] << " - " << i << "\t" << key[i] << endl;
        totalWeight += key[i];
    }
    cout << "Total Weight of MST: " << totalWeight << endl;
}

int main() {
    int V, E;
    cout << "Enter number of vertices: ";
    cin >> V;
    cout << "Enter number of edges: ";
    cin >> E;
    
    // Create an adjacency list representation of the graph
    vector<vector<Edge>> graph(V);
    
    cout << "Enter edges (source destination weight):" << endl;
    for (int i = 0; i < E; i++) {
        int src, dest, weight;
        cin >> src >> dest >> weight;
        
        // Add edge to graph (undirected graph, so add both ways)
        graph[src].push_back({dest, weight});
        graph[dest].push_back({src, weight});
    }
    
    // Find MST using Prim's algorithm
    primMST(graph, V);
    
    return 0;
}