POJ2485 Highways

来源:互联网 发布:mac多余账户删不了 编辑:程序博客网 时间:2024/05/21 13:08

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.
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
Hint

Huge input,scanf is recommended.

题目传送门

n个点,每个点之间有一条边,求1~n的最短路中的最长边

最小生成树,kruskal算法可以秒A
很舒服啊,模版改一下就好,难度低一点的就是个板子,刷的有点上瘾
求最短路的最长边啊,跟POJ1251一样,把模版改一下就好。
具体看代码不啰嗦
代码如下:

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;struct node{    int x,y,d;    node(){x=0;y=0;d=0;}}a[1100000];int len;int cmp(const void*x1,const void*x2){    node n1=*(node*)x1;node n2=*(node*)x2;    return n1.d-n2.d;}int fa[1100000];int findfa(int x){    if(fa[x]!=x)return findfa(fa[x]);    return x;}int main(){    int n,T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        len=0;int d;        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)            {                   scanf("%d",&d);                if(d!=0){len++;a[len].x=i,a[len].y=j,a[len].d=d;}            }        for(int i=1;i<=n;i++)fa[i]=i;        qsort(a+1,len,sizeof(node),cmp);        int t=0;int ans=0;        for(int i=1;i<=len;i++)        {            int x=a[i].x,y=a[i].y;            int fx=findfa(x),fy=findfa(y);            if(fx!=fy)            {                fa[fy]=fx;                t++;            }            if(t==n-1)            {                ans=a[i].d;                break;            }        }        printf("%d\n",ans);    }    return 0;}

by_lmy