sicily 1090 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 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. 

Output

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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

分析:

本题旨在求最小生成树中权值最大的边,套用最小生成树算法,使用变量存储边的值即可。需注意,本体可能出现边非常稠密的状况,权衡判断还是Prim算法更好,Kruskal更适合边稀疏的图。

代码:

// Problem#: 1090
// Submission#: 1892154
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
using namespace std;

#define MAX 500
#define INF 65536
unsigned int dist[MAX][MAX];
unsigned int tmp[MAX];

inline int min( int n ){
    unsigned int min = INF;
    int re;
    for( int i=0 ; i<n ; i++ ){
        if( tmp[i]>0  && tmp[i]<min ){
            re = i;
            min = tmp[i];
        }
    }
    return re;
}

int mst_prim( int n ){
    unsigned int re = 0;
    int k;
    for( int i=0 ; i<n ; i++ )
        tmp[i] = dist[0][i];
    for( int i=1 ; i<n ; i++ ){
        k = min(n);
        re = tmp[k]>re ? tmp[k] : re;
        tmp[k] = 0;
        for( int j=0 ; j<n ; j++ )
            if( dist[k][j] < tmp[j] )
                tmp[j] = dist[k][j];
    }
    return re;
}

int main(){
    int t,n;
    cin >> t;
    while(t--){
        cin >> n;
        for( int i=0 ; i<n ; i++ )
            for( int j=0 ; j<n ; j++ )
                cin >> dist[i][j];

        cout << mst_prim(n) << endl;
        if( t ) cout << endl;
    }
    return 0;
}

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