[POJ2485]Highways

来源:互联网 发布:防网络诈骗 编辑:程序博客网 时间:2024/06/05 23:08

Time Limit: 1000MS Memory Limit: 65536K

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

TIPS:跟你们讲个笑话“Huge input”(:-D)

题意:求最小生成树上的最长的边的边长。

题解:就当复习prim了

#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<algorithm>#include<queue>#define LiangJiaJun main#define INF 1999122700#define pa pair<int,int>using namespace std;int T;int mp[504][504],n,mcs[504];bool vis[504];int w33ha(){    memset(mp,0,sizeof(mp));    memset(vis,0,sizeof(vis));    int ans=0;    scanf("%d",&n);    for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++){            scanf("%d",&mp[i][j]);            if(mp[i][j]==0)mp[i][j]=INF;        }    for(int i=1;i<=n;i++)mcs[i]=INF;    mcs[1]=0;    for(int i=1;i<=n;i++){        int x=-1;        for(int j=1;j<=n;j++)            if(!vis[j] &&(x == -1 || mcs[x] > mcs[j]))x=j;        if(x==-1)break;        vis[x]=1;ans=max(ans,mcs[x]);        for(int j=1;j<=n;j++){            mcs[j]=min(mcs[j],mp[x][j]);        }    }    cout<<ans<<endl;    return 0;}int LiangJiaJun(){    scanf("%d",&T);    while(T--)w33ha();    return 0;}
原创粉丝点击