Skip to content

Commit b9800af

Browse files
committed
feat: update solutions to lc problems
* No.1135.Connecting Cities With Minimum Cost * No.1168.Optimize Water Distribution in a Village
1 parent 1171c97 commit b9800af

File tree

5 files changed

+8
-165
lines changed

5 files changed

+8
-165
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115

116116
- [网络延迟时间](/solution/0700-0799/0743.Network%20Delay%20Time/README.md) - 最短路、Dijkstra 算法、Bellman Ford 算法、SPFA 算法
117117
- [连接所有点的最小费用](/solution/1500-1599/1584.Min%20Cost%20to%20Connect%20All%20Points/README.md) - 最小生成树、Prim 算法、Kruskal 算法
118+
- [最低成本联通所有城市](/solution/1100-1199/1135.Connecting%20Cities%20With%20Minimum%20Cost/README.md) - 最小生成树、Kruskal 算法
119+
- [水资源分配优化](/solution/1100-1199/1168.Optimize%20Water%20Distribution%20in%20a%20Village/README.md) - 最小生成树、Kruskal 算法
118120

119121
<!-- 待补充
120122
### 6. 数学知识

README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
111111

112112
- [Network Delay Time](/solution/0700-0799/0743.Network%20Delay%20Time/README_EN.md) - Shortest Path, Dijkstra's algorithm, Bellman Ford's algorithm, SPFA
113113
- [Min Cost to Connect All Points](/solution/1500-1599/1584.Min%20Cost%20to%20Connect%20All%20Points/README_EN.md) - Minimum Spanning Tree, Prim's algorithm, Kruskal's algorithm
114+
- [Connecting Cities With Minimum Cost](/solution/1100-1199/1135.Connecting%20Cities%20With%20Minimum%20Cost/README_EN.md) - Minimum Spanning Tree, Kruskal's algorithm
115+
- [Optimize Water Distribution in a Village](/solution/1100-1199/1168.Optimize%20Water%20Distribution%20in%20a%20Village/README_EN.md) - Minimum Spanning Tree, Kruskal's algorithm
114116

115117
<!--
116118
### 6. Mathematical Knowledge

solution/1100-1199/1135.Connecting Cities With Minimum Cost/README.md

+2-59
Original file line numberDiff line numberDiff line change
@@ -53,66 +53,9 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
最小生成树 + 并查集
56+
最小生成树问题
5757

58-
并查集模板:
59-
60-
模板 1——朴素并查集:
61-
62-
```python
63-
# 初始化,p存储每个点的父节点
64-
p = list(range(n))
65-
66-
# 返回x的祖宗节点
67-
def find(x):
68-
if p[x] != x:
69-
# 路径压缩
70-
p[x] = find(p[x])
71-
return p[x]
72-
73-
# 合并a和b所在的两个集合
74-
p[find(a)] = find(b)
75-
```
76-
77-
模板 2——维护 size 的并查集:
78-
79-
```python
80-
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
81-
p = list(range(n))
82-
size = [1] * n
83-
84-
# 返回x的祖宗节点
85-
def find(x):
86-
if p[x] != x:
87-
# 路径压缩
88-
p[x] = find(p[x])
89-
return p[x]
90-
91-
# 合并a和b所在的两个集合
92-
if find(a) != find(b):
93-
size[find(b)] += size[find(a)]
94-
p[find(a)] = find(b)
95-
```
96-
97-
模板 3——维护到祖宗节点距离的并查集:
98-
99-
```python
100-
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
101-
p = list(range(n))
102-
d = [0] * n
103-
104-
# 返回x的祖宗节点
105-
def find(x):
106-
if p[x] != x:
107-
t = find(p[x])
108-
d[x] += d[p[x]]
109-
p[x] = t
110-
return p[x]
111-
112-
# 合并a和b所在的两个集合
113-
p[find(a)] = find(b)
114-
d[find(a)] = distance
115-
```
58+
**方法一:Kruskal 算法**
11659

11760
<!-- tabs:start -->
11861

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

+2-57
Original file line numberDiff line numberDiff line change
@@ -65,64 +65,9 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68-
并查集
68+
最小生成树问题
6969

70-
模板 1——朴素并查集:
71-
72-
```python
73-
# 初始化,p存储每个点的父节点
74-
p = list(range(n))
75-
76-
# 返回x的祖宗节点
77-
def find(x):
78-
if p[x] != x:
79-
# 路径压缩
80-
p[x] = find(p[x])
81-
return p[x]
82-
83-
# 合并a和b所在的两个集合
84-
p[find(a)] = find(b)
85-
```
86-
87-
模板 2——维护 size 的并查集:
88-
89-
```python
90-
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
91-
p = list(range(n))
92-
size = [1] * n
93-
94-
# 返回x的祖宗节点
95-
def find(x):
96-
if p[x] != x:
97-
# 路径压缩
98-
p[x] = find(p[x])
99-
return p[x]
100-
101-
# 合并a和b所在的两个集合
102-
if find(a) != find(b):
103-
size[find(b)] += size[find(a)]
104-
p[find(a)] = find(b)
105-
```
106-
107-
模板 3——维护到祖宗节点距离的并查集:
108-
109-
```python
110-
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
111-
p = list(range(n))
112-
d = [0] * n
113-
114-
# 返回x的祖宗节点
115-
def find(x):
116-
if p[x] != x:
117-
t = find(p[x])
118-
d[x] += d[p[x]]
119-
p[x] = t
120-
return p[x]
121-
122-
# 合并a和b所在的两个集合
123-
p[find(a)] = find(b)
124-
d[find(a)] = distance
125-
```
70+
**方法一:kruskal 算法**
12671

12772
对于本题,可以将节点 0 视为水库,水库到房子间的成本等于房子内建造水井的成本。因此此题可以转换为最小生成树问题。
12873

solution/1500-1599/1584.Min Cost to Connect All Points/S.go

-49
This file was deleted.

0 commit comments

Comments
 (0)