Skip to content

Commit 8922a04

Browse files
committed
feat: add solutions to lc problem: No.1135.Connecting Cities With Minimum Cost
1 parent 9b2d447 commit 8922a04

File tree

6 files changed

+493
-4
lines changed

6 files changed

+493
-4
lines changed

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

+208-2
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,233 @@
4646
<li><code>conections[i][0] != conections[i][1]</code></li>
4747
</ol>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
5352

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

56116
### **Python3**
57117

58118
<!-- 这里可写当前语言的特殊实现逻辑 -->
59119

60120
```python
61-
121+
class Solution:
122+
def minimumCost(self, n: int, connections: List[List[int]]) -> int:
123+
p = list(range(n))
124+
connections.sort(key=lambda x: x[2])
125+
res = 0
126+
127+
def find(x):
128+
if p[x] != x:
129+
p[x] = find(p[x])
130+
return p[x]
131+
132+
def union(a, b):
133+
pa, pb = find(a - 1), find(b - 1)
134+
if pa == pb:
135+
return False
136+
p[pa] = pb
137+
return True
138+
139+
for c1, c2, cost in connections:
140+
if union(c1, c2):
141+
n -= 1
142+
res += cost
143+
if n == 1:
144+
return res
145+
return -1
62146
```
63147

64148
### **Java**
65149

66150
<!-- 这里可写当前语言的特殊实现逻辑 -->
67151

68152
```java
153+
class Solution {
154+
private int[] p;
155+
156+
public int minimumCost(int n, int[][] connections) {
157+
p = new int[n];
158+
for (int i = 0; i < n; ++i) {
159+
p[i] = i;
160+
}
161+
Arrays.sort(connections, (a, b) -> a[2] - b[2]);
162+
int res = 0;
163+
for (int[] e : connections) {
164+
if (union(e[0], e[1])) {
165+
res += e[2];
166+
--n;
167+
if (n == 1) {
168+
return res;
169+
}
170+
}
171+
}
172+
return -1;
173+
}
174+
175+
private int find(int x) {
176+
if (p[x] != x) {
177+
p[x] = find(p[x]);
178+
}
179+
return p[x];
180+
}
181+
182+
private boolean union(int a, int b) {
183+
int pa = find(a - 1), pb = find(b - 1);
184+
if (pa == pb) {
185+
return false;
186+
}
187+
p[pa] = pb;
188+
return true;
189+
}
190+
}
191+
```
192+
193+
### **C++**
194+
195+
```cpp
196+
class Solution {
197+
public:
198+
vector<int> p;
199+
200+
int minimumCost(int n, vector<vector<int>> &connections) {
201+
p.resize(n);
202+
for (int i = 0; i < n; ++i) p[i] = i;
203+
auto cmp = [](auto &a, auto &b)
204+
{
205+
return a[2] < b[2];
206+
};
207+
sort(connections.begin(), connections.end(), cmp);
208+
int res = 0;
209+
for (auto e : connections)
210+
{
211+
if (unite(e[0], e[1]))
212+
{
213+
res += e[2];
214+
--n;
215+
if (n == 1) return res;
216+
}
217+
}
218+
return -1;
219+
}
220+
221+
int find(int x) {
222+
if (p[x] != x) p[x] = find(p[x]);
223+
return p[x];
224+
}
225+
226+
bool unite(int a, int b) {
227+
int pa = find(a - 1), pb = find(b - 1);
228+
if (pa == pb) return false;
229+
p[pa] = pb;
230+
return true;
231+
}
232+
};
233+
```
69234
235+
### **Go**
236+
237+
```go
238+
var p []int
239+
240+
func minimumCost(n int, connections [][]int) int {
241+
p = make([]int, n)
242+
for i := 0; i < len(p); i++ {
243+
p[i] = i
244+
}
245+
sort.Slice(connections, func(i, j int) bool {
246+
return connections[i][2] < connections[j][2]
247+
})
248+
res := 0
249+
for _, e := range connections {
250+
if union(e[0], e[1]) {
251+
res += e[2]
252+
n--
253+
if n == 1 {
254+
return res
255+
}
256+
}
257+
}
258+
return -1
259+
}
260+
261+
func find(x int) int {
262+
if p[x] != x {
263+
p[x] = find(p[x])
264+
}
265+
return p[x]
266+
}
267+
268+
func union(a, b int) bool {
269+
pa, pb := find(a-1), find(b-1)
270+
if pa == pb {
271+
return false
272+
}
273+
p[pa] = pb
274+
return true
275+
}
70276
```
71277

72278
### **...**

0 commit comments

Comments
 (0)