POJ 2485 Highways

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29076   Accepted: 13271

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.

Source

POJ Contest,Author:Mathematica@ZSU
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,fa[10000],t;
 7 struct node {
 8     int from;
 9     int to;
10     int value;
11     bool operator < (const node &a) const
12     {
13         return value<a.value;
14     }
15 }e[502*502];
16 int find(int x)
17 {
18     if(fa[x]==x) return x;
19     return fa[x]=find(fa[x]);
20 }
21 int main()
22 {
23     scanf("%d",&t);
24     while(t--)
25     {
26     scanf("%d",&n);
27     int cnt=0,v;
28     for(int i=1;i<=n;i++)
29     {
30         fa[i]=i;
31         for(int j=1;j<=n;j++)
32           {
33               scanf("%d",&v);
34                if(v!=0)
35                {
36                  cnt++;
37                  e[cnt].value=v;
38                    e[cnt].from=i;
39                    e[cnt].to=j;
40                }
41           }
42     }
43       
44     sort(e+1,e+cnt+1);
45       
46     int cut=0;int mst=0;
47     while(cut<=cnt)
48     {
49         cut++;
50         int rx=find(e[cut].from);int ry=find(e[cut].to);
51         if(rx!=ry)
52         {
53             fa[rx]=ry;
54             mst=max(e[cut].value,mst);
55         }
56     }
57     printf("%d\n",mst);    
58     
59     }
60     return 0;
61 }

思路:Kruskal 模板题 注意输出格式给 我刚开始忘了换行  直接把我搞瘫了

posted @ 2016-10-17 11:31  浮华的终成空  阅读(133)  评论(0编辑  收藏  举报

Contact with me