POJ 2485 Highways 无向图最小生成树求最长的边

来源:互联网 发布:java判断目录是否存在 编辑:程序博客网 时间:2024/06/06 02:54
点击打开链接

Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19239 Accepted: 8933

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

130 990 692990 0 179692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU

题意是说要修一些高速公路,要求所有城市必须联通。所求的是最长的公路是多长。也就是求这棵树的最长边是多少。
用Kruskal算法,在排完序求边长的时候进行找最长的边即可。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#define MAX 60005using namespace std;typedef struct{    int v1,v2,distance;} Edge;Edge roads[MAX+1];int  tree[11007];bool cmp(const Edge&a,const Edge&b){    return a.distance<b.distance;}int Find(int x){    while(x!=tree[x])        x=tree[x];    int u=x,v;    while(tree[u]!=x)    {        v=tree[u];        tree[u]=x;        u=v;    }    return x;}void Union(int x,int y){    int fx=Find(x),fy=Find(y);    if(fx!=fy)        tree[fx]=fy;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,q,a,b,i,j,k,tmp,sum;        scanf("%d",&n);        k=0;        for(i=1; i<=n; ++i)        {            for(j=1; j<=n; ++j)            {                scanf("%d",&tmp);                roads[k].v1=i;                roads[k].v2=j;                roads[k].distance=tmp;                ++k;            }        }        for(i=0; i<11000; ++i)        {            tree[i]=i;        }        int maxx=-1;        sort(roads,roads+k,cmp);        for(i=0,sum=0; i<k; ++i)        {            if(Find(roads[i].v1)!=Find(roads[i].v2))            {                Union(roads[i].v1,roads[i].v2);                if(maxx<roads[i].distance)//找最小生成树最长的边                    maxx=roads[i].distance;//记录下来            }        }        printf("%d\n",maxx);    }    return 0;}


原创粉丝点击