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 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  } 
时间: 2024-12-18 19:02:25

POJ 2485 Highways (求最小生成树中最大的边)的相关文章

poj 2485 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 plann

POJ 2485 Highways【最小生成树最大边,Prime算法】

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

poj 2485 Highways (最小生成树)

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

POJ 2485 Highways (prim最小生成树)

对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的图做一次最小生成树 去树上的最长边就可以 #include<bits/stdc++.h> using namespace std; int T,n,a,dist[1020],m[1020][1020]; void prim() { bool p[1020]; for(int i=2;i<=n

POJ 2485 Highways【最小生成树】

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

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 &amp;&amp; HDU1102(20/200)

题目链接:Highways 没看题,看了输入输出,就有种似曾相识的感觉,果然和HDU1102 题相似度99%,但是也遇到一坑 cin输入竟然TLE,cin的缓存不至于这么狠吧,题目很水,矩阵已经告诉你了,就敲个模板就是了,5分钟,1A 题意就是打印,最小生成树的最大边权,改了改输入,水过 这个题完了,我的个人POJ计划进度以完成 20/200,这其中主要是图论的题,等下周把POJ计划图论的题目打完,就回头打模拟!我就不信还能比图论难 #include <iostream> #include &

POJ 2485 Highways (kruskal 最小生成树)

Highways POJ 2485so 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 ca

POJ #2485 Highways MST中的最大边权

Description 问题描述:链接 思路 题目很直接,容易看出建的图是一张完全图,需要求出图中最小生成树的最大边权.由于数据上界已经定死了,那么选择用静态邻接表存建图.由于图是稠密图,那么求MST的话就选择 prim . 做完 POJ #2253 Frogger 变种Dijkstra 后再做这个就很有感觉了.其实prim中的核心操作就是记录并更新点v到树的最短距离,然后把所有最短距离之中的最小值选出,将该点其加入树集.这个树集与dijkstra中的点集是异曲同工的. 能用 scanf 的话尽