POJ 2485 Highways 解题报告

分类:图论,最小生成树
作者: ACShiryu
时间:2011-7-29
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12754 Accepted: 5969

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
题目大意就是求最小生成树的最大权边,关于求法不再重复,详情请见本博客的今天和昨天题目的解题报告,可以看到此题的类似解法,可以直接把那代码贴过来,改两行就行了。
1Y

参考代码:

 1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 int map[500][500];
9 int lowcost[500];
10 const int inf = (1<<20) ;
11 int main()
12 {
13 int n;
14 int t;
15 cin >> t;
16 while ( t -- )
17 {
18 cin >> n ;
19 int i , j ;
20 for ( i = 0 ; i < n ; i ++ )
21 for ( j = 0 ; j < n ; j ++ )
22 cin >> map[i][j];
23 for ( i = 0 ; i < n ; i ++ )
24 lowcost[i]=map[0][i]; //初始化各点到集合的距离
25 int ans=0;//记录生成树的最大权值
26 for ( i = 0 ; i < n-1 ; i ++ )
27 {
28 int mindis=inf;
29 int minone;
30 for ( j = 0 ; j < n ; j ++ )
31 {//寻找到集合距离最近的点
32 if(lowcost[j]&&mindis>lowcost[j])
33 {
34 mindis=lowcost[j];
35 minone=j;
36 }
37 }
38 if(ans <mindis )
39 ans =mindis;
40 lowcost[minone]=0;
41 for ( j = 0 ; j < n ; j ++ )
42 {//更新各点到集合的距离
43 if(lowcost[j]>map[minone][j])
44 lowcost[j]=map[minone][j];
45 }
46 }
47 cout<<ans<<endl;
48 }
49 return 0;
50 }

  

你可能感兴趣的:(poj)