Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 751a9f0

Browse files
feat: add solutions to lc problem: No.1168 (#2052)
No.1168.Optimize Water Distribution in a Village
1 parent dcffdef commit 751a9f0

File tree

8 files changed

+1024
-191
lines changed

8 files changed

+1024
-191
lines changed

‎solution/1100-1199/1168.Optimize Water Distribution in a Village/README.md‎

Lines changed: 364 additions & 30 deletions
Large diffs are not rendered by default.

‎solution/1100-1199/1168.Optimize Water Distribution in a Village/README_EN.md‎

Lines changed: 364 additions & 30 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
1-
class Solution {
2-
public:
3-
int minCostToSupplyWater(int n, vector<int>& wells, vector<vector<int>>& pipes) {
4-
for (int i = 0; i < n; ++i) {
5-
pipes.push_back({0, i + 1, wells[i]});
6-
}
7-
sort(pipes.begin(), pipes.end(), [](const vector<int>& a, const vector<int>& b) {
8-
return a[2] < b[2];
9-
});
10-
int p[n + 1];
11-
iota(p, p + n + 1, 0);
12-
function<int(int)> find = [&](int x) {
13-
if (p[x] != x) {
14-
p[x] = find(p[x]);
15-
}
16-
return p[x];
17-
};
18-
int ans = 0;
19-
for (const auto& x : pipes) {
20-
int pa = find(x[0]), pb = find(x[1]);
21-
if (pa == pb) {
22-
continue;
23-
}
24-
p[pa] = pb;
25-
ans += x[2];
26-
if (--n == 0) {
27-
break;
28-
}
29-
}
30-
return ans;
31-
}
1+
class UnionFind {
2+
public:
3+
UnionFind(int n) {
4+
p = vector<int>(n);
5+
size = vector<int>(n, 1);
6+
iota(p.begin(), p.end(), 0);
7+
}
8+
9+
bool unite(int a, int b) {
10+
int pa = find(a), pb = find(b);
11+
if (pa == pb) {
12+
return false;
13+
}
14+
if (size[pa] > size[pb]) {
15+
p[pb] = pa;
16+
size[pa] += size[pb];
17+
} else {
18+
p[pa] = pb;
19+
size[pb] += size[pa];
20+
}
21+
return true;
22+
}
23+
24+
int find(int x) {
25+
if (p[x] != x) {
26+
p[x] = find(p[x]);
27+
}
28+
return p[x];
29+
}
30+
31+
private:
32+
vector<int> p, size;
33+
};
34+
35+
class Solution {
36+
public:
37+
int minCostToSupplyWater(int n, vector<int>& wells, vector<vector<int>>& pipes) {
38+
for (int i = 0; i < n; ++i) {
39+
pipes.push_back({0, i + 1, wells[i]});
40+
}
41+
sort(pipes.begin(), pipes.end(), [](const vector<int>& a, const vector<int>& b) {
42+
return a[2] < b[2];
43+
});
44+
UnionFind uf(n + 1);
45+
int ans = 0;
46+
for (const auto& x : pipes) {
47+
if (uf.unite(x[0], x[1])) {
48+
ans += x[2];
49+
if (--n == 0) {
50+
break;
51+
}
52+
}
53+
}
54+
return ans;
55+
}
3256
};

‎solution/1100-1199/1168.Optimize Water Distribution in a Village/Solution.go‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
1-
func minCostToSupplyWater(n int, wells []int, pipes [][]int) (ans int) {
2-
for i, w := range wells {
3-
pipes = append(pipes, []int{0, i + 1, w})
4-
}
5-
sort.Slice(pipes, func(i, j int) bool { return pipes[i][2] < pipes[j][2] })
6-
p := make([]int, n+1)
1+
type unionFind struct {
2+
p, size []int
3+
}
4+
5+
func newUnionFind(n int) *unionFind {
6+
p := make([]int, n)
7+
size := make([]int, n)
78
for i := range p {
89
p[i] = i
10+
size[i] = 1
911
}
10-
varfindfunc(int) int
11-
find=func(xint) int {
12-
ifp[x] !=x {
13-
p[x] =find(p[x])
14-
}
15-
returnp[x]
12+
return&unionFind{p, size}
13+
}
14+
15+
func (uf*unionFind) find(xint) int {
16+
ifuf.p[x] !=x {
17+
uf.p[x] =uf.find(uf.p[x])
1618
}
19+
return uf.p[x]
20+
}
1721

22+
func (uf *unionFind) union(a, b int) bool {
23+
pa, pb := uf.find(a), uf.find(b)
24+
if pa == pb {
25+
return false
26+
}
27+
if uf.size[pa] > uf.size[pb] {
28+
uf.p[pb] = pa
29+
uf.size[pa] += uf.size[pb]
30+
} else {
31+
uf.p[pa] = pb
32+
uf.size[pb] += uf.size[pa]
33+
}
34+
return true
35+
}
36+
37+
func minCostToSupplyWater(n int, wells []int, pipes [][]int) (ans int) {
38+
for i, w := range wells {
39+
pipes = append(pipes, []int{0, i + 1, w})
40+
}
41+
sort.Slice(pipes, func(i, j int) bool { return pipes[i][2] < pipes[j][2] })
42+
uf := newUnionFind(n + 1)
1843
for _, x := range pipes {
19-
pa, pb := find(x[0]), find(x[1])
20-
if pa == pb {
21-
continue
22-
}
23-
p[pa] = pb
24-
ans += x[2]
25-
n--
26-
if n == 0 {
27-
break
44+
if uf.union(x[0], x[1]) {
45+
ans += x[2]
46+
n--
47+
if n == 0 {
48+
break
49+
}
2850
}
2951
}
3052
return
Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
1-
class Solution {
2-
private int[] p;
3-
4-
public int minCostToSupplyWater(int n, int[] wells, int[][] pipes) {
5-
var nums = new int[n + pipes.length][0];
6-
int j = 0;
7-
for (var pipe : pipes) {
8-
nums[j++] = pipe;
9-
}
10-
for (int i = 0; i < n; ++i) {
11-
nums[j++] = new int[] {0, i + 1, wells[i]};
12-
}
13-
Arrays.sort(nums, (a, b) -> a[2] - b[2]);
14-
p = new int[n + 1];
15-
for (int i = 1; i <= n; ++i) {
16-
p[i] = i;
17-
}
18-
int ans = 0;
19-
for (var x : nums) {
20-
int pa = find(x[0]), pb = find(x[1]);
21-
if (pa == pb) {
22-
continue;
23-
}
24-
ans += x[2];
25-
p[pa] = pb;
26-
if (--n == 0) {
27-
break;
28-
}
29-
}
30-
return ans;
31-
}
32-
33-
private int find(int x) {
34-
if (p[x] != x) {
35-
p[x] = find(p[x]);
36-
}
37-
return p[x];
38-
}
1+
class UnionFind {
2+
private int[] p;
3+
private int[] size;
4+
5+
public UnionFind(int n) {
6+
p = new int[n];
7+
size = new int[n];
8+
for (int i = 0; i < n; ++i) {
9+
p[i] = i;
10+
size[i] = 1;
11+
}
12+
}
13+
14+
public int find(int x) {
15+
if (p[x] != x) {
16+
p[x] = find(p[x]);
17+
}
18+
return p[x];
19+
}
20+
21+
public boolean union(int a, int b) {
22+
int pa = find(a), pb = find(b);
23+
if (pa == pb) {
24+
return false;
25+
}
26+
if (size[pa] > size[pb]) {
27+
p[pb] = pa;
28+
size[pa] += size[pb];
29+
} else {
30+
p[pa] = pb;
31+
size[pb] += size[pa];
32+
}
33+
return true;
34+
}
35+
}
36+
37+
class Solution {
38+
public int minCostToSupplyWater(int n, int[] wells, int[][] pipes) {
39+
int[][] nums = Arrays.copyOf(pipes, pipes.length + n);
40+
for (int i = 0; i < n; i++) {
41+
nums[pipes.length + i] = new int[] {0, i + 1, wells[i]};
42+
}
43+
Arrays.sort(nums, (a, b) -> a[2] - b[2]);
44+
UnionFind uf = new UnionFind(n + 1);
45+
int ans = 0;
46+
for (var x : nums) {
47+
int a = x[0], b = x[1], c = x[2];
48+
if (uf.union(a, b)) {
49+
ans += c;
50+
if (--n == 0) {
51+
break;
52+
}
53+
}
54+
}
55+
return ans;
56+
}
3957
}
Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
class Solution:
2-
def minCostToSupplyWater(
3-
self, n: int, wells: List[int], pipes: List[List[int]]
4-
) -> int:
5-
def find(x: int) -> int:
6-
if p[x] != x:
7-
p[x] = find(p[x])
8-
return p[x]
9-
10-
for i, w in enumerate(wells, 1):
11-
pipes.append([0, i, w])
12-
pipes.sort(key=lambda x: x[2])
13-
p = list(range(n + 1))
14-
ans = 0
15-
for i, j, c in pipes:
16-
pa, pb = find(i), find(j)
17-
if pa == pb:
18-
continue
19-
p[pa] = pb
20-
ans += c
21-
n -= 1
22-
if n == 0:
23-
break
24-
return ans
1+
class UnionFind:
2+
__slots__ = ("p", "size")
3+
4+
def __init__(self, n):
5+
self.p = list(range(n))
6+
self.size = [1] * n
7+
8+
def find(self, x: int) -> int:
9+
if self.p[x] != x:
10+
self.p[x] = self.find(self.p[x])
11+
return self.p[x]
12+
13+
def union(self, a: int, b: int) -> bool:
14+
pa, pb = self.find(a), self.find(b)
15+
if pa == pb:
16+
return False
17+
if self.size[pa] > self.size[pb]:
18+
self.p[pb] = pa
19+
self.size[pa] += self.size[pb]
20+
else:
21+
self.p[pa] = pb
22+
self.size[pb] += self.size[pa]
23+
return True
24+
25+
26+
class Solution:
27+
def minCostToSupplyWater(
28+
self, n: int, wells: List[int], pipes: List[List[int]]
29+
) -> int:
30+
for i, w in enumerate(wells, 1):
31+
pipes.append([0, i, w])
32+
pipes.sort(key=lambda x: x[2])
33+
uf = UnionFind(n + 1)
34+
ans = 0
35+
for a, b, c in pipes:
36+
if uf.union(a, b):
37+
ans += c
38+
n -= 1
39+
if n == 0:
40+
return ans
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
struct UnionFind {
2+
p: Vec<usize>,
3+
size: Vec<usize>,
4+
}
5+
6+
impl UnionFind {
7+
fn new(n: usize) -> Self {
8+
let p: Vec<usize> = (0..n).collect();
9+
let size = vec![1; n];
10+
UnionFind { p, size }
11+
}
12+
13+
fn find(&mut self, x: usize) -> usize {
14+
if self.p[x] != x {
15+
self.p[x] = self.find(self.p[x]);
16+
}
17+
self.p[x]
18+
}
19+
20+
fn union(&mut self, a: usize, b: usize) -> bool {
21+
let pa = self.find(a);
22+
let pb = self.find(b);
23+
if pa == pb {
24+
false
25+
} else if self.size[pa] > self.size[pb] {
26+
self.p[pb] = pa;
27+
self.size[pa] += self.size[pb];
28+
true
29+
} else {
30+
self.p[pa] = pb;
31+
self.size[pb] += self.size[pa];
32+
true
33+
}
34+
}
35+
}
36+
37+
impl Solution {
38+
pub fn min_cost_to_supply_water(n: i32, wells: Vec<i32>, pipes: Vec<Vec<i32>>) -> i32 {
39+
let n = n as usize;
40+
let mut pipes = pipes;
41+
for i in 0..n {
42+
pipes.push(vec![0, (i + 1) as i32, wells[i]]);
43+
}
44+
pipes.sort_by(|a, b| a[2].cmp(&b[2]));
45+
let mut uf = UnionFind::new(n + 1);
46+
let mut ans = 0;
47+
for pipe in pipes {
48+
let a = pipe[0] as usize;
49+
let b = pipe[1] as usize;
50+
let c = pipe[2];
51+
if uf.union(a, b) {
52+
ans += c;
53+
if n == 0 {
54+
break;
55+
}
56+
}
57+
}
58+
ans
59+
}
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /