-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1584. Min Cost to Connect All Points.cpp
74 lines (71 loc) · 2.37 KB
/
1584. Min Cost to Connect All Points.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
public:
// MST : using vector :1811ms , using pair<int,int>: 181ms
int minCostConnectPoints1(vector<vector<int>>& points) {
int n = points.size();
vector<int> visited(n,false);
//priority_queue<vector<int>,vector<vector<int>>, greater<>> pq;
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<>> pq;
pq.push({0,0});
int cost=0;
int count = 0;
while(!pq.empty())
{
auto v = pq.top();
pq.pop();
int& node = v.second;
if(visited[node])
continue;
visited[node] = true;
cost += v.first;
++count;
if(count==n)
break;
for(int i=0;i<n;++i)
{
if(!visited[i] && i!=node)
{
int d = abs(points[node][0] - points[i][0]) + abs(points[node][1] - points[i][1]);
pq.push({d,i});
}
}
}
return cost;
}
// Prims's clean : 185ms
int minCostConnectPoints2(vector<vector<int>>& ps) {
int n = ps.size(), res = 0, i = 0, connected = 0;
vector<bool> visited(n);
priority_queue<pair<int, int>> pq;
while (++connected < n) {
visited[i] = true;
for (int j = 0; j < n; ++j)
if (!visited[j])
pq.push({-(abs(ps[i][0] - ps[j][0]) + abs(ps[i][1] - ps[j][1])), j});
while (visited[pq.top().second])
pq.pop();
res -= pq.top().first;
i = pq.top().second;
pq.pop();
}
return res;
}
//Prim's for Complete Graph , track the min distance for each node
// 100ms
int minCostConnectPoints(vector<vector<int>>& ps) {
int n = ps.size(), res = 0, i = 0, connected = 0;
vector<int> min_d(n, 10000000);
while (++connected < n) {
min_d[i] = INT_MAX;
int min_j = i;
for (int j = 0; j < n; ++j)
if (min_d[j] != INT_MAX) {
min_d[j] = min(min_d[j], abs(ps[i][0] - ps[j][0]) + abs(ps[i][1] - ps[j][1]));
min_j = min_d[j] < min_d[min_j] ? j : min_j;
}
res += min_d[min_j];
i = min_j;
}
return res;
}
};