-
Notifications
You must be signed in to change notification settings - Fork 0
/
1135. Connecting Cities With Minimum Cost
51 lines (39 loc) · 1.13 KB
/
1135. Connecting Cities With Minimum Cost
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
//union find
class Solution {
private:
unordered_map<int, int> parent;
int count;
int find(int i) {
if(parent[i] == i) return i;
parent[i] = find(parent[i]);
return parent[i];
}
bool merge(int i, int j) {
int pi = find(i), pj = find(j);
if(pi == pj)
return false;
count--;
parent[pi] = pj;
return true;
}
public:
int minimumCost(int N, vector<vector<int>>& connections) {
if(connections.size() < N - 1)
return -1;
auto comp = [&](vector<int> &a, vector<int> &b) {
return a[2] < b[2];
};
count = N;
sort(connections.begin(), connections.end(), comp);
int cost = 0;
for(int i = 1; i <= N; i++)
parent[i] = i;
for(auto &x : connections) {
if(count == 1) return cost;
int cur = x[0], neigh = x[1];
if(merge(cur, neigh))
cost += x[2];
}
return count == 1 ? cost : -1;
}
};