forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1168.cpp
33 lines (31 loc) · 873 Bytes
/
1168.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
class Solution
{
public:
int minCostToSupplyWater(int n, vector<int>& wells, vector<vector<int>>& pipes)
{
for (int i = 0; i < wells.size(); ++i) pipes.push_back({0, i+1, wells[i]});
for (int i = 0; i <= n; ++i) parent.push_back(i);
sort(pipes.begin(), pipes.end(), [](const vector<int>& a, const vector<int>& b) {
return a[2] < b[2];
});
int e = 0, res = 0, k = 0;
while (e < n)
{
auto pre = pipes[k++];
int x = find(pre[0]), y = find(pre[1]);
if (x != y)
{
parent[x] = y;
e++;
res += pre[2];
}
}
return res;
}
vector<int> parent;
int find(int x)
{
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
};