-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim.cpp
More file actions
59 lines (53 loc) · 1.33 KB
/
Prim.cpp
File metadata and controls
59 lines (53 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Implementation of Prim's algorithm using priority queue
//
// Running time: O((E + V)logV)
#include<bits/stdc++.h>
using namespace std;
#define INF 1LL<<60
#define int long long
#define pii pair<int, int>
const int N = 100005;
int V, E;
bool vis[N];
int parent[N];
vector<pii> adj[N];
void Prim(int src) {
priority_queue<pii, vector<pii>, greater<pii> > pq;
int i, u, v, wt, cost;
for(i = 1 ; i <= V ; i++) {
vis[i] = false;
parent[i] = -1;
}
cost = 0;
pq.push(make_pair(0, src));
while(!pq.empty()) {
wt = pq.top().first;
u = pq.top().second;
pq.pop();
if(vis[u])
continue;
vis[u] = true;
cost += wt;
pq.pop();
for(i = 0 ; i < adj[u].size() ; i++) {
v = adj[u][i].second;
if(!vis[v])
pq.push(adj[u][i]);
}
}
cout << "Total cost of the MST: " << cost << endl;
}
int32_t main() {
int i, u, v, wt;
cout << "Enter no of vertices:";
cin >> V;
cout << "Enter the no of edges:";
cin >> E;
for(i = 0 ; i < E ; i ++) {
cin >> u >> v >> wt; // An edge between u - v with cost wt
adj[u].push_back(make_pair(wt, v));
adj[v].push_back(make_pair(wt, u));
}
Prim(1); // Considering 1 as source node
return 0;
}