MST Prim Algorithm

The MST Prim Algorithm is a popular graph algorithm that is used to find the minimum spanning tree (MST) for a connected, undirected graph with weighted edges. Developed by Czech mathematician Vojtěch Jarník in 1930 and later independently by computer scientist Robert C. Prim in 1957, the algorithm efficiently constructs the MST by progressively adding edges to the tree while ensuring that the newly added edge connects to a new vertex and has the minimal weight among all edges connecting the tree to the rest of the graph. The underlying principle of the algorithm is to maintain a set of visited vertices and a set of unvisited vertices, and iteratively select the edge with the smallest weight that connects a visited vertex to an unvisited vertex, until all vertices have been visited. To implement the Prim's algorithm, one can use various data structures, such as adjacency matrices, adjacency lists, or priority queues. The algorithm's performance can also be improved by using Fibonacci heaps or binary heaps for faster execution. The process begins by selecting an arbitrary starting vertex, marking it as visited, and adding its neighboring edges to the priority queue. Then, in each step, the algorithm dequeues the edge with the lowest weight, adds the edge to the MST, marks the newly connected vertex as visited, and inserts its unvisited neighboring edges into the priority queue. This iterative process continues until all vertices have been visited, and the resulting MST is a tree that spans all vertices in the graph with the minimum possible total edge weight. Prim's algorithm is guaranteed to find the optimal solution and has a time complexity of O(V²) for adjacency matrices, and O(E + V log V) for adjacency lists with a binary heap, where V represents the number of vertices and E represents the number of edges in the graph.
/**************************************************************************************

    Minimum spanning tree using Prim algorithm. O(N ^ 2)

    Based on problem 185 from informatics.mccme.ru:
    http://informatics.mccme.ru/mod/statements/view3.php?id=261&chapterid=185

**************************************************************************************/

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>

using namespace std;

const long long INF = (long long) 1e12;
const int MAXN = 1050;

int n, m;
long long e[MAXN][MAXN];
long long dist[MAXN];
bool used[MAXN];
long long ans;

int main() {
    //assert(freopen("input.txt","r",stdin));
    //assert(freopen("output.txt","w",stdout));

    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            e[i][j] = INF;

    for (int i = 1; i <= m; i++) {
        int from, to, w;
        scanf("%d %d %d", &from, &to, &w);
        e[from][to] = e[to][from] = w;
    }

    dist[1] = 0;
    for (int i = 2; i <= n; i++)
        dist[i] = INF;

    for (int i = 1; i <= n; i++) {
        int cur = -1;
        for (int j = 1; j <= n; j++)
            if (!used[j] && (cur == -1 || dist[j] < dist[cur])) {
                cur = j;
            }

        used[cur] = true;
        ans += dist[cur];

        for (int j = 1; j <= n; j++) {
            if (used[j])
                continue;
            if (dist[j] > e[cur][j])
                dist[j] = e[cur][j];
        } 
    }                           

    cout << ans << endl;

    return 0;
}

LANGUAGE:

DARK MODE: