码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2485 Highways (求最小生成树中最大的边)

时间:2016-08-03 22:17:42      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

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

 1 /*题目大意:描述 :有个城市叫做H市。其中有很多个村庄,村庄之间通信基本靠吼,
 2 交通基本靠走,很不方便。这个市长知道了这个情况,为了替市民着想,决定修建高铁。
 3 每修建一米花费1美元。现在市长请了最著名的工程师来修建高铁,自然这个工程师会让修建高铁的费用最少。
 4 不幸的是,在修建了高铁之后就病逝了。现在市长希望知道在修建完成的这些高铁路中最小生成树中最大的边的值。 
 5 他请你来帮助他,如果你计算正确,市长将会送你一辆兰博基尼。
 6 输入:
 7 第一行一个数T,表示接下来有多少组数据。
 8 接下来每组测试数据的第一行有一个数N(3<=N<=500),表示村庄数目。然后是一个二维数组,
 9 第i行第j列表示第i个村庄到第j个村庄的距离。
10 
11 输出:
12 只有一个数,输出市长希望知道的已经修成的高铁中最小生成树中最大的边的值。*/
13 #include<cstdio>
14 #include<algorithm>
15 using namespace std;
16 int fa[50000];
17 struct stu
18 {
19     int from,to,al;
20 }st[50000];
21 bool cmp(stu a,stu b)
22 {
23     return a.al<b.al;
24 }
25 int find(int a)
26 {
27     int r=a;
28     while(r != fa[r])
29     {
30         r=fa[r];
31     }
32     return r;
33 }
34 int main()
35 {
36     int t;
37     scanf("%d",&t);
38     while(t--)
39     {
40         int i,j,a,k,n,min=0;
41         int num=-1;
42         scanf("%d",&n);
43         for(i = 1 ; i <= n ; i++)
44         {
45             fa[i]=i;
46         }
47         for(i = 1 ; i <= n ; i++)
48         {
49             for(j = 1 ; j <= n ; j++)
50             {
51                 scanf("%d",&a);
52                 if(i < j)                //i到j的距离和j到i的距离一样,避免重复保存 
53                 {
54                     num++;
55                     st[num].from=i;
56                     st[num].to=j;
57                     st[num].al=a;
58                 }
59             }
60         }
61         k=0;
62         sort(st,st+num+1,cmp);        //按两地间的距离从小到大排序 
63         for(i = 0 ; i <= num ; i++)
64         {
65             if(k == n-1)             //n个节点需要n-1个边 
66             {
67                 break;
68             }
69             if(find(st[i].from) != find(st[i].to))
70             {
71                 fa[find(st[i].from)] = find(st[i].to);
72                  k++;
73             }
74         }
75         printf("%d\n",st[i-1].al);        //因为按两地距离最近的开始找,所以找到n-1个有效边的时,第n-1个边最大 
76     }
77  } 

 

POJ 2485 Highways (求最小生成树中最大的边)

标签:

原文地址:http://www.cnblogs.com/yexiaozi/p/5734495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!