-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path1584. Min Cost to Connect All Points.cpp
200 lines (162 loc) · 5.28 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//priority_queue + dsu
//TLE
//71 / 72 test cases passed.
class DSU{
public:
int n;
vector<int> parent;
DSU(int n){
this->n = n;
parent = vector<int>(n);
iota(parent.begin(), parent.end(), 0);
}
int find(int x){
if(parent[x] != x){
parent[x] = find(parent[x]);
}
return parent[x];
}
void unite(int x, int y){
int px = find(x);
int py = find(y);
parent[py] = px;
}
bool isConnected(int x, int y){
return (find(x) == find(y));
}
};
class Solution {
public:
int getDist(vector<int>& pi, vector<int>& pj){
return abs(pi[0]-pj[0]) + abs(pi[1]-pj[1]);
}
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
if(n == 1) return 0;
// vector<vector<int>> graph(n, vector<int>(n, INT_MAX));
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;
for(int i = 0; i < n; ++i){
// graph[i][i] = 0;
vector<int>& pi = points[i];
for(int j = i+1; j < n; ++j){
vector<int>& pj = points[j];
int dist = getDist(pi, pj);
// graph[i][j] = graph[j][i] = dist;
pq.push({dist, i, j});
}
}
//edges needed
int need = n-1;
int ans = 0;
DSU dsu(n);
while(need > 0){
vector<int> edge = pq.top(); pq.pop();
int dist = edge[0], src = edge[1], dst = edge[2];
if(dsu.isConnected(src, dst))
continue;
dsu.unite(src, dst);
ans += dist;
--need;
}
return ans;
}
};
//Minimum spanning tree, Prim’s Algorithm
//https://www.geeksforgeeks.org/prims-minimum-spanning-tree-mst-greedy-algo-5/
//https://www.geeksforgeeks.org/minimum-cost-connect-cities/
//ARRAY
//Runtime: 80 ms, faster than 100.00% of C++ online submissions for Min Cost to Connect All Points.
//Memory Usage: 26.6 MB, less than 25.00% of C++ online submissions for Min Cost to Connect All Points.
//vector
//Runtime: 188 ms, faster than 50.00% of C++ online submissions for Min Cost to Connect All Points.
//Memory Usage: 26.7 MB, less than 25.00% of C++ online submissions for Min Cost to Connect All Points.
//time: O(V^2)
#define ARRAY
class Solution {
public:
int getDist(vector<int>& pi, vector<int>& pj){
return abs(pi[0]-pj[0]) + abs(pi[1]-pj[1]);
}
#ifdef ARRAY
int minnode(bool mstset[], int dist[], int n){
#else
int minnode(vector<bool>& mstset, vector<int>& dist){
int n = mstset.size();
#endif
int minv = INT_MAX;
int mini = -1;
for(int i = 0; i < n; ++i){
if(!mstset[i] && dist[i] < minv){
minv = dist[i];
mini = i;
}
}
return mini;
}
int primMST(vector<vector<int>>& graph){
int n = graph.size();
#ifdef ARRAY
bool mstset[n];
int parent[n];
int dist[n];
//memset only work for char!
//it interpret its second argument as "unsigned char" and set n "bytes"!
// memset(mstset, false, n);
// memset(parent, -1, n);
// memset(dist, INT_MAX, n);
fill(mstset, mstset+n, false);
fill(parent, parent+n, -1);
fill(dist, dist+n, INT_MAX);
#else
vector<bool> mstset(n, false); //whether is in MST
vector<int> parent(n, -1);
vector<int> dist(n, INT_MAX); //distance to MST
#endif
// mstset[0] = true;
parent[0] = -1;
dist[0] = 0;
/*
in each iteration, we add one node into MST,
and then update dist and parent,
we only need to do n-1 iterations,
because for the last node, it won't update dist and parent
*/
for(int i = 0; i < n-1; ++i){
#ifdef ARRAY
int u = minnode(mstset, dist, n);
#else
int u = minnode(mstset, dist);
#endif
mstset[u] = true;
//update dist and parent with the newly added "u"
for(int v = 0; v < n; ++v){
//u and v are always connected in this problem
if(!mstset[v] && graph[u][v] < dist[v]){
dist[v] = graph[u][v];
parent[v] = u;
}
}
}
int cost = 0;
//note here i starts from 1
for(int i = 1; i < n; ++i){
cost += graph[parent[i]][i];
}
return cost;
}
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
if(n == 1) return 0;
vector<vector<int>> graph(n, vector<int>(n, INT_MAX));
for(int i = 0; i < n; ++i){
// graph[i][i] = 0;
vector<int>& pi = points[i];
for(int j = i+1; j < n; ++j){
vector<int>& pj = points[j];
int dist = getDist(pi, pj);
graph[i][j] = graph[j][i] = dist;
}
}
return primMST(graph);
}
};