POJ 2485 Highways

来源:互联网 发布:邮箱验证正则表达式 js 编辑:程序博客网 时间:2024/06/08 05:39
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26693 Accepted: 12218

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

思路:简单的最小生成树,但是要注意用scanf输入,用cin会超时
AC代码1,prim算法
#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define INF 0x3f3f3f3fconst int maxn=500+5;int mp[maxn][maxn];int dis[maxn],vis[maxn];void prim(int n){    for(int i=0;i<n;i++)        dis[i]=mp[0][i];    vis[0]=1,dis[0]=0;    int ans=-1;    for(int i=1;i<n;i++){        int minn=INF,p;        for(int j=0;j<n;j++){            if(!vis[j] && minn>dis[j]){                minn=dis[j];                p=j;            }        }        vis[p]=1;        if(ans<minn)            ans=minn;        for(int j=0;j<n;j++)        if(!vis[j] && mp[p][j]<dis[j])            dis[j]=mp[p][j];    }    printf("%d\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--){        int n;        memset(vis,0,sizeof(vis));        scanf("%d",&n);        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)            scanf("%d",&mp[i][j]);        prim(n);    }    return 0;}
AC代码2:kruskal算法:
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;#define INF 0x3f3f3f3fconst int maxn=500+5;int dis[maxn],vis[maxn];struct Node{    int x,y,val;}path[maxn*maxn];int fa[maxn];int myfind(int x){    if(fa[x]==x) return x;    else return fa[x]=myfind(fa[x]);}bool cmp(Node a,Node b){    return a.val<b.val;}void kruskal(int n){    int ans=-1;    for(int i=0;i<n;i++){        int tx=myfind(path[i].x);        int ty=myfind(path[i].y);        if(tx!=ty){            if(path[i].val>ans)                ans=path[i].val;            fa[tx]=ty;        }    }    printf("%d\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--){        int n;        int cnt=0;        scanf("%d",&n);        for(int i=0;i<=maxn;i++) fa[i]=i;        for(int i=0;i<n;i++)            for(int j=0;j<n;j++){                int x;                scanf("%d",&x);                path[cnt].x=i,path[cnt].y=j,path[cnt].val=x;                cnt++;            }        sort(path,path+cnt,cmp);        kruskal(cnt);    }    return 0;}



0 0