Created
April 14, 2017 07:27
-
-
Save kumarmj/607abb63b7c4f79598220eea3499bade to your computer and use it in GitHub Desktop.
Nice implementation of prims minimum spanning tree algorithm https://www.hackerearth.com/practice/algorithms/graphs/minimum-spanning-tree/tutorial/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <queue> | |
#include <functional> | |
#include <utility> | |
using namespace std; | |
const int MAX = 1e4 + 5; | |
typedef pair<long long, int> PII; | |
bool marked[MAX]; | |
vector <PII> adj[MAX]; | |
long long prim(int x) | |
{ | |
priority_queue<PII, vector<PII>, greater<PII> > Q; | |
int y; | |
long long minimumCost = 0; | |
PII p; | |
Q.push(make_pair(0, x)); | |
while(!Q.empty()) | |
{ | |
// Select the edge with minimum weight | |
p = Q.top(); | |
Q.pop(); | |
x = p.second; | |
// Checking for cycle | |
if(marked[x] == true) | |
continue; | |
minimumCost += p.first; | |
marked[x] = true; | |
for(int i = 0;i < adj[x].size();++i) | |
{ | |
y = adj[x][i].second; | |
if(marked[y] == false) | |
Q.push(adj[x][i]); | |
} | |
} | |
return minimumCost; | |
} | |
int main() | |
{ | |
int nodes, edges, x, y; | |
long long weight, minimumCost; | |
cin >> nodes >> edges; | |
for(int i = 0;i < edges;++i) | |
{ | |
cin >> x >> y >> weight; | |
adj[x].push_back(make_pair(weight, y)); | |
adj[y].push_back(make_pair(weight, x)); | |
} | |
// Selecting 1 as the starting node | |
minimumCost = prim(1); | |
cout << minimumCost << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment