Highways

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.
本以为有坑,结果似乎是我想多了  并查集优先队列解决  
 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<cstring>
16 int n;
17 int a[510];
18 int find (int x)
19 {
20     if(a[x]==x)
21         return x;
22     return a[x]=find(a[x]);
23 }
24 int balabalanengliang(int x,int y)
25 {
26     int x1=find(x);
27     int y1=find(y);
28     if(x1==y1)
29         return 1;
30     a[y1]=x1;
31     return 0;
32 }
33 struct lll
34 {
35     int x,y,len;
36 }s;
37 struct cmp1{
38     bool operator ()(lll &a,lll &b){
39         return a.len>b.len;//最小值优先
40     }
41 };
42 priority_queue<lll,vector<lll>,cmp1>TM;
43 
44 int main()
45 {
46     int t;
47     cin>>t;
48     while(t--)
49     {
50         cin>>n;
51         memset(a,0,sizeof(a));
52         for(int i=1;i<=n;i++)
53         {
54             for(int j=1;j<=n;j++)
55             {
56                 a[j]=j;
57                 int t;
58                 cin>>t;
59                 if(i==j)
60                     continue;
61                 s.x=i;
62                 s.y=j;
63                 s.len=t;
64                 TM.push(s);
65             }
66         }
67         int temp;
68         while(!TM.empty())
69         {
70             if(balabalanengliang(TM.top().x,TM.top().y))
71             {
72                TM.pop();
73                continue;
74             }
75             temp=TM.top().len;
76             TM.pop();
77         }
78         cout<<temp<<endl;
79     }
80     return 0;
81 }
View Code

 

posted @ 2017-08-06 18:36  小小超plus  阅读(239)  评论(0编辑  收藏  举报