main
黄海 2 years ago
parent de5aac9881
commit 4bc693c75d

@ -4,11 +4,11 @@ const int N = 2010, M = 10010;
int n, m, p[N];
struct Edge {
int a, b, w;
int a, b, c;
bool operator<(const Edge &t) const {
return w < t.w;
return c < t.c;
}
} e[M];
} edge[M];
int el;
int find(int x) {
@ -31,18 +31,18 @@ int main() {
p[find(a)] = find(b), res += c;
else
// 记录可选边有哪些
e[el++] = {a, b, c};
edge[el++] = {a, b, c};
}
// 对可选边进行由小到大排序
sort(e, e + el);
sort(edge, edge + el);
// 枚举每条可选边
for (int i = 0; i < el; i++) {
int a = find(e[i].a), b = find(e[i].b), c = e[i].w;
int a = find(edge[i].a), b = find(edge[i].b), c = edge[i].c;
if (a != b) p[a] = b, res += c;
}
// 输出最短长度
printf("%d\n", res);
cout << res << endl;
return 0;
}

@ -51,11 +51,11 @@ const int N = 2010, M = 10010;
int n, m, p[N];
struct Edge {
int a, b, w;
int a, b, c;
bool operator<(const Edge &t) const {
return w < t.w;
return c < t.c;
}
} e[M];
} edge[M];
int el;
int find(int x) {
@ -78,19 +78,19 @@ int main() {
p[find(a)] = find(b), res += c;
else
// 记录可选边有哪些
e[el++] = {a, b, c};
edge[el++] = {a, b, c};
}
// 对可选边进行由小到大排序
sort(e, e + el);
sort(edge, edge + el);
// 枚举每条可选边
for (int i = 0; i < el; i++) {
int a = find(e[i].a), b = find(e[i].b), c = e[i].w;
int a = find(edge[i].a), b = find(edge[i].b), c = edge[i].c;
if (a != b) p[a] = b, res += c;
}
// 输出最短长度
printf("%d\n", res);
cout << res << endl;
return 0;
}
```

@ -146,9 +146,11 @@ $Prim$或者$Kruskal$祼题,直接套模板即可
最小生成森林,需要注意与最小生成树的区别,两种方法,推荐使用$Kruskal$
#### [$AcWing$ $1142$. 繁忙的都市](https://www.cnblogs.com/littlehb/p/16044984.html)
$Kruskal$的简单应用,求最小生成树
#### [$AcWing$ $1143$. 联络员](https://www.cnblogs.com/littlehb/p/16048583.html)
$Kruskal$的简单应用,先把必选的边放到并查集中,然后将可选的边由小到大排序,再进行$Kruskal$即可。
AcWing 1143. 联络员
AcWing 1144. 连接格点

Loading…
Cancel
Save