'commit'
This commit is contained in:
129
TangDou/AcWing_TiGao/T3/MinialSpanningTree/1142.md
Normal file
129
TangDou/AcWing_TiGao/T3/MinialSpanningTree/1142.md
Normal file
@@ -0,0 +1,129 @@
|
||||
## [$AcWing$ $1142$. 繁忙的都市](https://www.acwing.com/problem/content/1144/)
|
||||
|
||||
### 一、题目描述
|
||||
城市$C$是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。
|
||||
|
||||
城市$C$的道路是这样分布的:
|
||||
|
||||
城市中有 $n$ 个交叉路口,编号是 $1$∼$n$,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连接。
|
||||
|
||||
这些道路是 **双向** 的,且把所有的交叉路口直接或间接的连接起来了。
|
||||
|
||||
每条道路都有一个分值,**分值越小** 表示这个道路 **越繁忙**,越需要进行改造。
|
||||
|
||||
但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的要求:
|
||||
|
||||
1.改造的那些道路能够把所有的交叉路口直接或间接的连通起来。
|
||||
|
||||
2.在满足要求$1$的情况下,改造的道路尽量少。
|
||||
|
||||
3.在满足要求$1、2$的情况下,改造的那些道路中分值 **最大值尽量小**。
|
||||
|
||||
作为市规划局的你,应当作出最佳的决策,选择哪些道路应当被修建。
|
||||
|
||||
**输入格式**
|
||||
第一行有两个整数 $n,m$ 表示城市有 $n$ 个交叉路口,$m$ 条道路。
|
||||
|
||||
接下来 $m$ 行是对每条道路的描述,每行包含三个整数$u,v,c$ 表示交叉路口 $u$ 和 $v$ 之间有道路相连,分值为 $c$。
|
||||
|
||||
**输出格式**
|
||||
两个整数 $s,max$,表示你选出了几条道路,分值最大的那条道路的分值是多少。
|
||||
|
||||
|
||||
### 二、$Kruskal$算法
|
||||
```cpp {.line-numbers}
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
const int N = 310, M = 8010;
|
||||
|
||||
// Kruskal用到的结构体
|
||||
struct Node {
|
||||
int a, b, c;
|
||||
bool const operator<(const Node &t) const {
|
||||
return c < t.c; // 边权小的在前
|
||||
}
|
||||
} edge[M];
|
||||
|
||||
int n, m;
|
||||
int p[N];
|
||||
|
||||
int find(int x) {
|
||||
if (p[x] != x) p[x] = find(p[x]);
|
||||
return p[x];
|
||||
}
|
||||
|
||||
int res;
|
||||
|
||||
// Kruskal算法
|
||||
void kruskal() {
|
||||
// 1、按边权由小到大排序
|
||||
sort(edge, edge + m);
|
||||
// 2、并查集初始化
|
||||
for (int i = 1; i <= n; i++) p[i] = i;
|
||||
// 3、迭代m次
|
||||
for (int i = 0; i < m; i++) {
|
||||
int a = edge[i].a, b = edge[i].b, c = edge[i].c;
|
||||
a = find(a), b = find(b);
|
||||
if (a != b)
|
||||
p[a] = b, res = c; // 越往后越大
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
edge[i] = {a, b, c};
|
||||
}
|
||||
kruskal();
|
||||
printf("%d %d\n", n - 1, res);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 三、$Prim$算法
|
||||
```cpp {.line-numbers}
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
const int N = 310;
|
||||
int dis[N];
|
||||
int g[N][N];
|
||||
int n, m;
|
||||
bool st[N];
|
||||
|
||||
int prim() {
|
||||
memset(dis, 0x3f, sizeof dis);
|
||||
dis[1] = 0;
|
||||
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int t = -1;
|
||||
for (int j = 1; j <= n; j++)
|
||||
if (!st[j] && (t == -1 || dis[t] > dis[j]))
|
||||
t = j;
|
||||
res = max(res, dis[t]); // 找出最长,不要累加和
|
||||
for (int j = 1; j <= n; j++)
|
||||
if (!st[j] && dis[j] > g[t][j])
|
||||
dis[j] = g[t][j];
|
||||
st[t] = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
memset(g, 0x3f, sizeof g);
|
||||
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
g[a][b] = g[b][a] = min(g[a][b], c);
|
||||
}
|
||||
printf("%d %d\n", n - 1, prim());
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user