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.
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.
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更适合边稀疏的图。
代码:
1 // Problem#: 1090 2 // Submission#: 1892154 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include <iostream> 7 using namespace std; 8 9 #define MAX 500 10 #define INF 65536 11 unsigned int dist[MAX][MAX]; 12 unsigned int tmp[MAX]; 13 14 inline int min( int n ){ 15 unsigned int min = INF; 16 int re; 17 for( int i=0 ; i<n ; i++ ){ 18 if( tmp[i]>0 && tmp[i]<min ){ 19 re = i; 20 min = tmp[i]; 21 } 22 } 23 return re; 24 } 25 26 int mst_prim( int n ){ 27 unsigned int re = 0; 28 int k; 29 for( int i=0 ; i<n ; i++ ) 30 tmp[i] = dist[0][i]; 31 for( int i=1 ; i<n ; i++ ){ 32 k = min(n); 33 re = tmp[k]>re ? tmp[k] : re; 34 tmp[k] = 0; 35 for( int j=0 ; j<n ; j++ ) 36 if( dist[k][j] < tmp[j] ) 37 tmp[j] = dist[k][j]; 38 } 39 return re; 40 } 41 42 int main(){ 43 int t,n; 44 cin >> t; 45 while(t--){ 46 cin >> n; 47 for( int i=0 ; i<n ; i++ ) 48 for( int j=0 ; j<n ; j++ ) 49 cin >> dist[i][j]; 50 51 cout << mst_prim(n) << endl; 52 if( t ) cout << endl; 53 } 54 return 0; 55 }