C - 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

题目意思:和畅通工程差不多,只是要输出最长的那条路:
解题思路:最小生成树
 1 #include <iostream>
 2 #include <queue>
 3 #include <string.h>
 4 #include <stdio.h>
 5 #include <math.h>
 6 using namespace std;
 7
 8 const int MAX = 500 + 100;
 9 int visit[MAX];
10 int n;
11
12 struct S
13 {
14     int a,b;
15     int len;
16 };
17
18 struct cmp
19 {
20     bool operator() ( S a,S b )
21     {
22         return a.len>b.len;
23     }
24 };
25
26 int Find (int x)
27 {
28     if(x == visit[x])
29         return x;
30     else
31         return visit[x] = Find(visit[x]);
32 }
33
34 int mix(int x,int y)
35 {
36     int Tx = Find(x);
37     int Ty = Find(y);
38     if(Tx != Ty)
39     {
40         visit[Tx] = Ty;
41         return 1;
42     }
43     return 0;
44
45 }
46
47 int main()
48 {
49     int N;
50     cin>>N;
51
52     while(N--)
53     {
54         cin>>n;
55         int Map[MAX][MAX];
56         for(int i = 1;i <= n;i++)
57         {
58             visit[i] = i;
59             for(int j = 1;j <= n;j++)
60                 cin>>Map[i][j];
61         }
62
63         S temp;
64         priority_queue<S,vector<S>,cmp>P;
65         for(int i = 1;i < n;i++)
66             for(int j = i+1;j <=n;j++)
67             {
68                temp.a = i;temp.b = j;
69                temp.len = Map[i][j];
70                P.push(temp);
71             }
72
73         int Max = 0;
74         while(!P.empty())
75         {
76             temp = P.top();
77             P.pop();
78
79             if( mix(temp.a,temp.b)==1)
80             {
81                 Max = Max > temp.len?Max:temp.len;
82             }
83         }
84
85         cout<<Max<<endl;
86     }
87
88     return 0;
89 }
				
时间: 2024-07-30 15:27:32

C - Highways的相关文章

[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be c

UVA - 1393 Highways

Description Hackerland is a happy democratic country with m×n cities, arranged in a rectangular m by n grid and connected by m roads in the east-west direction and n roads in the north-south direction. By public demand, this orthogonal road system is

poj 2485 Highways

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断 比较最小生成树每条边的大小就行 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int cmp(struct

POJ 2485 Highways 最小生成树 (Kruskal)

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 i

POJ 2485 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 i

[2016-01-27][POJ][1751][B - Highways]

[2016-01-27][POJ][1751][B - Highways] B - Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1751 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a

POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23426   Accepted: 10829 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

POJ2485 Highways【Prim】

Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23247 Accepted: 10720 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatop

POJ2485:Highways(模板题)

http://poj.org/problem?id=2485 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 t

hdu 2485 Highways

题意:Flatopia岛要修路,这个岛上有n个城市,要求修完路后,各城市之间可以相互到达,且修的总路程最短.求所修路中的最长的路段 最小生成树的一道题,很裸的一道题,不知道为什么就是编译过不了. #include<iostream> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int w[200000],u[200000],v[200000],p[5