Description
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.
Input
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.
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.
Output
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.
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
题目大意:一个人当村长,想共享光纤,输入的是图,求最小生成树;这题和poj 1258 同一个代码,修改一行就可以
思路: 按无向图处理;
1.图转化成点到点的距离并储存起来
2.按权有小到大排序
3.Kruskal算法+并查集 构建最小生成树
#include<stdio.h>
#include<stdlib.h>
struct node
{
int i,j;
int weight;
} s[];
int cmp(const void*a,const void *b)
{
return (*(node*)a).weight-(*(node*)b).weight;
}
int v[];
int findl(int n)
{
return v[n]==n?n:findl(v[n]);
}
int main()
{
int m,n,i,j,k,a,sum;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
n++,k=,sum=;
for(i=; i<; i++)
v[i]=i;
for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
scanf("%d",&a);
if(j>i)//0及以下的数字直接忽略
{
s[k].weight=a;
s[k].i=i,s[k].j=j,k++;
}
}
}
qsort(s,k,sizeof(node),cmp);//按权值从小到大排序
for(i=; i<k; i++)
{
if(findl(s[i].i)!=findl(s[i].j))//简单的并查集
{
if(sum<s[i].weight)
sum=s[i].weight;
v[findl(s[i].i)]=s[i].j;
}
}
printf("%d\n",sum);
}
return ;
}