首页 > 代码库 > POJ2485 Highways

POJ2485 Highways

 
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

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
 
求最小生成树的最长边。
ans从累加改成存max即可。
 1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=510; 9 int n,m;10 int mp[mxn][mxn];11 int dis[mxn];12 bool vis[mxn];13 int ans=0;14 int main(){15     int i,j;16     int T;17     scanf("%d",&T);18     while(T--){19         scanf("%d",&n);20         ans=0;21         memset(mp,0x2f,sizeof mp);22         memset(dis,0x2f,sizeof dis);23         memset(vis,0,sizeof vis);24         int u,v,d;25         for(i=1;i<=n;i++)26          for(j=1;j<=n;j++){27              scanf("%d",&m);28              mp[i][j]=m;29              if(!m)mp[i][j]=1e6;30          }31         dis[1]=0;32         int pos,mini;33         while(1){34             pos=0;mini=0x2f2f2f2f;35             for(i=1;i<=n;i++){36                 if(!vis[i] && dis[i]<mini){37                     mini=dis[i];38                     pos=i;39                 }40             }41             if(pos==0)break;42             ans=max(ans,dis[pos]);dis[pos]=0;vis[pos]=1;43             for(i=1;i<=n;i++){44                 dis[i]=min(dis[i],mp[pos][i]);45             }46         }47         printf("%d\n",ans);48     }49     return 0;50 }

 

POJ2485 Highways