From 4bc693c75d4e9f06cfdad982edb3df201c33c7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Mon, 8 Jan 2024 14:22:14 +0800 Subject: [PATCH] 'commit' --- .../AcWing_TiGao/T3/MinialSpanningTree/1143.cpp | 14 +++++++------- TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.md | 14 +++++++------- TangDou/Topic/【最小生成树】专题.md | 4 +++- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.cpp b/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.cpp index 016077c..9cd09dc 100644 --- a/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.cpp +++ b/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.cpp @@ -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; } \ No newline at end of file diff --git a/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.md b/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.md index 4809240..0d0b097 100644 --- a/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.md +++ b/TangDou/AcWing_TiGao/T3/MinialSpanningTree/1143.md @@ -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; } ``` diff --git a/TangDou/Topic/【最小生成树】专题.md b/TangDou/Topic/【最小生成树】专题.md index b50aae0..6ff47fb 100644 --- a/TangDou/Topic/【最小生成树】专题.md +++ b/TangDou/Topic/【最小生成树】专题.md @@ -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. 连接格点