ACM-最小生成树之Highways——poj2485

*******************************************转载请注明出处:http://blog.csdn.net/lttree**********************************************


Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21061   Accepted: 9724

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.


题目:http://poj.org/problem?id=2485


MST的变种,就是求构成最小生成树的边中,长度最长的边。。。。

给的二维数组,显然的想到用Prim做。

将 sum(求权值和) 变量改为 longest

每次不必把权值相加,只需要比较,存当前为止最长的边,即可。


PS:最最最重要+坑爹的一点,题目中说 each test have empty line 

其实是没有的!!每组测试数据间没有空行!!!!!(PE N次啊)


/****************************************
*****************************************
*        Author:Tree                    *
*From :http://blog.csdn.net/lttree      *
* Title : Highways                      *
*Source: poj 2485                       *
* Hint  : 最小生成树(MST-Prim)       *
*****************************************
****************************************/

#include <stdio.h>

#define RANGE 501
#define MAX 0x7fffffff

int cost[RANGE][RANGE];
int mincost[RANGE];
bool used[RANGE];
int n;

int Min(int a,int b)
{
    return a<b?a:b;
}

void prim( void )
{
    int i,u,v,longest;
    longest=-1;
    // 以第一个点为起点,初始化mincost和used
    for( i=1;i<=n;++i )
    {
        used[i]=false;
        mincost[i]=cost[1][i];
    }

    while( true )
    {
        v=-1;
        for( u=1;u<=n;++u )
            if( !used[u] && (v==-1 || mincost[u]<mincost[v]) )
                v=u;
        // 没有可到达的点,则退出
        if( v==-1 ) break;

        used[v]=true;
        // 如果当前的边长度大于之前所有边长度则替换
        longest= mincost[v]>longest?mincost[v]:longest;

        for( u=1;u<=n;++u )
            mincost[u]=Min( mincost[u],cost[v][u] );
    }
    // 输出
    printf("%d\n",longest);
}

int main()
{
    int i,j,t;
    scanf("%d",&t);

    while( t-- )
    {
        scanf("%d",&n);
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
                scanf("%d",&cost[i][j]);
        prim();
    }
    return 0;
}


你可能感兴趣的:(最小生成树,ACM,Prim,Highways,poj2485)