题目描述
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
输入
The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.
输出
For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
样例输入
1
3
0 990 692
990 0 179
692 179 0
样例输出
692
算法1
prim
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510, INF = 0x3f3f3f3f;
int g[N][N];
int dist[N];
bool st[N];
int n;
int prim()
{
int maxv = 0, res = 0; //前者记录最大的边,后者记录最小生成树的最小权值
memset(st, false, sizeof st);
for(int i = 0; i<n; i++) //每个点都用来作为更新的点
{
int t = -1;
for(int j = 1; j <= n; j++)
{
if(!st[j]) //如果该点还没做过更新点
if(t == -1 || dist[t] > dist[j]) //找到目前距离集合最近的点
t = j;
}
if(i && dist[t] == INF) return INF; //目前最近的点距离集合无穷远,而且不是初试点,不能最小生成树
if(dist[t] != INF )
maxv = max(maxv, dist[t]);
if(i) res += dist[t]; //在其他点更新之前更新res,防止有’自负‘环,使dist[t]被反复更新
//更新距离
for(int i = 1; i <= n; i++)
{
dist[i] = min(dist[i], g[t][i]); //t点放进集合里,用其与更新点的距离来比较原距离
}
st[t] = true; //该点已经做为过更新点
}
return maxv;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
memset(g, 0x3f, sizeof g);
memset(dist, 0x3f, sizeof dist);
for(int i = 1; i<=n; i++)
for(int j = 1; j<=n; j++)
scanf("%d", &g[i][j]);
int x = prim();
if(x == INF) printf("impossible\n");
else printf("%d\n", x);
}
return 0;
}
算法2
kruskal
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 510;
const int M = N * (N - 1) / 2; // 最大可能的边数3 => 2+1; 4 => 3+2+1
int f[N];
struct Edge {
int a, b, w;
bool operator< (const Edge &e) const {
return w < e.w;
}
} edge[M];
int find(int x) {
if (f[x] != x) f[x] = find(f[x]);
return f[x];
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, num = 0, res = 0;
scanf("%d", &n);
priority_queue<int> s; //大根堆存储最大边
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int w;
scanf("%d", &w);
if (j > i) {
edge[num].w = w; //用num来记录
edge[num].a = i + 1;
edge[num++].b = j + 1;
}
}
}
for (int i = 1; i <= n; i++) {
f[i] = i;
}
sort(edge, edge + num); // 修改了排序范围
int sum = 1; //最短的已经默认a点在集合里了
for (int i = 0; i < num; i++) {
int a = edge[i].a, b = edge[i].b, w = edge[i].w;
int t1 = find(a), t2 = find(b);
if (t1 != t2) {
f[t2] = t1;
res += w;
s.push(w);
sum++;
}
if (sum == n - 1) break; // 一旦加入足够的边就可以停止
}
if (sum != n)
printf("impossible\n");
else
printf("%d\n", s.top());
}
return 0;
}