-
Notifications
You must be signed in to change notification settings - Fork 753
/
2. Prims.cpp
61 lines (44 loc) · 1.31 KB
/
2. Prims.cpp
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
/*
Notes:
1- No self loop or parelled edges
2- We try to connect all the nodes
3- We start by picking 1 node
4- Then we select the smallest edge from this node
5- Now we have 2 nodes
6- Now we select the min edge from these 2 nodes
7- This way we keep including nodes and covers up all nodes
8- In total we have V nodes and V-1 edges
9- We use adj list
Time Complexity :- O((V+E)Log(V))
Space Complexity :- O(E+V)
*/
#include<bits/stdc++.h>
using namespace std;
int minCostConnectPoints(vector<vector<int>>& points) {
int V = points.size();
vector<int> mst(V,0);
vector<int> key(V,INT_MAX);
vector<int> parent(V,-1);
int ans=0;
priority_queue<vector<int>> q;
q.push({0,0});
key[0]=0;
while(q.size()) {
int u = q.top()[1];
mst[u]=1;
q.pop();
for(int v=0;v<V;v++) {
if(v == u || mst[v] == 1 ) continue;
int w = abs(points[u][0]-points[v][0]) + abs(points[u][1]-points[v][1]);
if(w < key[v]) {
key[v] = w;
parent[v]=u;
q.push({-key[v],v});
}
}
}
for(auto &a:key) {
ans+=a;
}
return ans;
}