53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
- 最小生成树问题。
56
+ 最小生成树问题。设 n 表示点数,m 表示边数。
57
57
58
58
** 方法一:Kruskal 算法**
59
59
60
+ 时间复杂度 O(mlogm)。
61
+
60
62
<!-- tabs:start -->
61
63
62
64
### ** Python3**
66
68
``` python
67
69
class Solution :
68
70
def minimumCost (self , n : int , connections : List[List[int ]]) -> int :
69
- p = list (range (n))
70
- connections.sort(key = lambda x : x[2 ])
71
- res = 0
72
-
73
71
def find (x ):
74
72
if p[x] != x:
75
73
p[x] = find(p[x])
76
74
return p[x]
77
75
78
- def union (a , b ):
79
- pa, pb = find(a - 1 ), find(b - 1 )
80
- if pa == pb:
81
- return False
82
- p[pa] = pb
83
- return True
84
-
85
- for c1, c2, cost in connections:
86
- if union(c1, c2):
87
- n -= 1
88
- res += cost
89
- if n == 1 :
90
- return res
76
+ connections.sort(key = lambda x : x[2 ])
77
+ p = list (range (n))
78
+ ans = 0
79
+ for x, y, cost in connections:
80
+ x, y = x - 1 , y - 1
81
+ if find(x) == find(y):
82
+ continue
83
+ p[find(x)] = find(y)
84
+ ans += cost
85
+ n -= 1
86
+ if n == 1 :
87
+ return ans
91
88
return - 1
92
89
```
93
90
@@ -100,19 +97,21 @@ class Solution {
100
97
private int [] p;
101
98
102
99
public int minimumCost (int n , int [][] connections ) {
100
+ Arrays . sort(connections, Comparator . comparingInt(a - > a[2 ]));
103
101
p = new int [n];
104
102
for (int i = 0 ; i < n; ++ i) {
105
103
p[i] = i;
106
104
}
107
- Arrays . sort(connections, (a, b) - > a[2 ] - b[2 ]);
108
- int res = 0 ;
105
+ int ans = 0 ;
109
106
for (int [] e : connections) {
110
- if (union(e[0 ], e[1 ])) {
111
- res += e[2 ];
112
- -- n;
113
- if (n == 1 ) {
114
- return res;
115
- }
107
+ int x = e[0 ] - 1 , y = e[1 ] - 1 , cost = e[2 ];
108
+ if (find(x) == find(y)) {
109
+ continue ;
110
+ }
111
+ p[find(x)] = find(y);
112
+ ans += cost;
113
+ if (-- n == 1 ) {
114
+ return ans;
116
115
}
117
116
}
118
117
return - 1 ;
@@ -124,15 +123,6 @@ class Solution {
124
123
}
125
124
return p[x];
126
125
}
127
-
128
- private boolean union (int a , int b ) {
129
- int pa = find(a - 1 ), pb = find(b - 1 );
130
- if (pa == pb) {
131
- return false ;
132
- }
133
- p[pa] = pb;
134
- return true ;
135
- }
136
126
}
137
127
```
138
128
@@ -143,23 +133,18 @@ class Solution {
143
133
public:
144
134
vector<int > p;
145
135
146
- int minimumCost(int n, vector<vector<int>> & connections) {
136
+ int minimumCost(int n, vector<vector<int>>& connections) {
147
137
p.resize(n);
148
138
for (int i = 0; i < n; ++i) p[i] = i;
149
- auto cmp = [](auto &a, auto &b)
150
- {
151
- return a[2] < b[2];
152
- };
153
- sort(connections.begin(), connections.end(), cmp);
154
- int res = 0;
155
- for (auto e : connections)
139
+ sort(connections.begin(), connections.end(), [](auto& a, auto& b) {return a[2] < b[2];});
140
+ int ans = 0;
141
+ for (auto& e : connections)
156
142
{
157
- if (unite(e[0], e[1]))
158
- {
159
- res += e[2];
160
- --n;
161
- if (n == 1) return res;
162
- }
143
+ int x = e[0] - 1, y = e[1] - 1, cost = e[2];
144
+ if (find(x) == find(y)) continue;
145
+ p[find(x)] = find(y);
146
+ ans += cost;
147
+ if (--n == 1) return ans;
163
148
}
164
149
return -1 ;
165
150
}
@@ -168,57 +153,42 @@ public:
168
153
if (p[ x] != x) p[ x] = find(p[ x] );
169
154
return p[ x] ;
170
155
}
171
-
172
- bool unite(int a, int b) {
173
- int pa = find(a - 1), pb = find(b - 1);
174
- if (pa == pb) return false;
175
- p[pa] = pb;
176
- return true;
177
- }
178
156
};
179
157
```
180
158
181
159
### **Go**
182
160
183
161
```go
184
- var p []int
185
-
186
162
func minimumCost(n int, connections [][]int) int {
187
- p = make([]int, n)
188
- for i := 0; i < len(p); i++ {
163
+ p : = make([]int, n)
164
+ for i := range p {
189
165
p[i] = i
190
166
}
191
167
sort.Slice(connections, func(i, j int) bool {
192
168
return connections[i][2] < connections[j][2]
193
169
})
194
- res := 0
170
+ var find func(x int) int
171
+ find = func(x int) int {
172
+ if p[x] != x {
173
+ p[x] = find(p[x])
174
+ }
175
+ return p[x]
176
+ }
177
+ ans := 0
195
178
for _, e := range connections {
196
- if union(e[0], e[1]) {
197
- res += e[2]
198
- n--
199
- if n == 1 {
200
- return res
201
- }
179
+ x, y, cost := e[0]-1, e[1]-1, e[2]
180
+ if find(x) == find(y) {
181
+ continue
182
+ }
183
+ p[find(x)] = find(y)
184
+ ans += cost
185
+ n--
186
+ if n == 1 {
187
+ return ans
202
188
}
203
189
}
204
190
return -1
205
191
}
206
-
207
- func find(x int) int {
208
- if p[x] != x {
209
- p[x] = find(p[x])
210
- }
211
- return p[x]
212
- }
213
-
214
- func union(a, b int) bool {
215
- pa, pb := find(a-1), find(b-1)
216
- if pa == pb {
217
- return false
218
- }
219
- p[pa] = pb
220
- return true
221
- }
222
192
```
223
193
224
194
### ** ...**
0 commit comments