POJ2485——Prim——Highways

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.

Source

POJ Contest,Author:Mathematica@ZSU
大意:有T组测试例,每一组先输入一个数表示村庄的个数,在下面n*n类似矩阵图形里面输入离本编号的距离比如0 990 692 就是编号为1的到1,到2,到3的距离.要求输出最短路径连通所有村庄后的最长的一条路是多少.-0-...不知道
There is an empty line after each test case.什么意思,在输入T后面输出一个回车结果pe去掉后就AC了
大循环n-1次
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f,MAX = 3000;
int map[MAX][MAX],k[MAX];
void prim (int map[MAX][MAX],int n)
{
    int m,min1;
   int  ans = 0;
     for(int i = 1; i <= n ;i++)
        k[i] = map[1][i];

     for(int i = 1; i < n ;i++){
            min1 = inf;
            m = 0;
         for(int j = 1; j <= n;j++){
              if(min1 > k[j]&&k[j]!=0){
                 min1 = k[j];
                 m = j;
              }
           }
           if(min1 > ans)
        ans = min1;
        k[m] = 0;
        for(int j = 1; j <= n ;j++){
             if(map[m][j] < k[j] && k[j] != 0)
                k[j] = map[m][j];
        }
     }
     printf("%d\n",ans);
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--){
            scanf("%d",&n);
            for(int i = 1; i <= n ;i++){
                 for(int j = 1; j <= n ;j++){
                        scanf("%d",&map[i][j]);
                 }
            }
            prim(map,n);
    }
    return 0;
}
View Code

 

posted @ 2015-03-08 15:11  Painting、时光  阅读(143)  评论(0编辑  收藏  举报