题目链接
洛谷题解: https://www.luogu.com.cn/problem/P2330
AcWing题解: https://www.acwing.com/problem/content/1144/
涉及知识
最小生成树和 Kruskal 算法
思路分析
关于 Kruskal 算法,由于它是基于贪心算法的,所以有以下两种作用:
1. 求解最小边权和
2. 求解最大边权的最小值
因此对于本题,要想道路分值的最大值最小,就可以直接套 Kruskal 算法模板,不断更新 r e s res res 即可,最后更新加入的边,即最大边权,一定可以保证其最小。
AC代码
cpp
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 8100;
struct Edge
{
int a, b, c;
bool operator <(const Edge &W) const
{
return c < W.c;
}
}edges[N];
int n, m, res;
int p[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
edges[i] = {a, b, c};
}
sort(edges + 1, edges + m + 1);
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 1; i <= m; i++)
{
int a = edges[i].a, b = edges[i].b, c = edges[i].c;
a = find(a), b = find(b);
if (a != b)
{
p[a] = b;
res = c;
}
}
printf("%d %d", n - 1, res);
return 0;
}