POJ 2485 Highways (求最小生成树中最大的边)
Description
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 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
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
/*题目大意:描述 :有个城市叫做H市。其中有很多个村庄,村庄之间通信基本靠吼,
交通基本靠走,很不方便。这个市长知道了这个情况,为了替市民着想,决定修建高铁。
每修建一米花费1美元。现在市长请了最著名的工程师来修建高铁,自然这个工程师会让修建高铁的费用最少。
不幸的是,在修建了高铁之后就病逝了。现在市长希望知道在修建完成的这些高铁路中最小生成树中最大的边的值。
他请你来帮助他,如果你计算正确,市长将会送你一辆兰博基尼。
输入:
第一行一个数T,表示接下来有多少组数据。
接下来每组测试数据的第一行有一个数N(3<=N<=500),表示村庄数目。然后是一个二维数组,
第i行第j列表示第i个村庄到第j个村庄的距离。 输出:
只有一个数,输出市长希望知道的已经修成的高铁中最小生成树中最大的边的值。*/
#include<cstdio>
#include<algorithm>
using namespace std;
int fa[];
struct stu
{
int from,to,al;
}st[];
bool cmp(stu a,stu b)
{
return a.al<b.al;
}
int find(int a)
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
return r;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,a,k,n,min=;
int num=-;
scanf("%d",&n);
for(i = ; i <= n ; i++)
{
fa[i]=i;
}
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
{
scanf("%d",&a);
if(i < j) //i到j的距离和j到i的距离一样,避免重复保存
{
num++;
st[num].from=i;
st[num].to=j;
st[num].al=a;
}
}
}
k=;
sort(st,st+num+,cmp); //按两地间的距离从小到大排序
for(i = ; i <= num ; i++)
{
if(k == n-) //n个节点需要n-1个边
{
break;
}
if(find(st[i].from) != find(st[i].to))
{
fa[find(st[i].from)] = find(st[i].to);
k++;
}
}
printf("%d\n",st[i-].al); //因为按两地距离最近的开始找,所以找到n-1个有效边的时,第n-1个边最大
}
}
POJ 2485 Highways (求最小生成树中最大的边)的更多相关文章
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- poj 2485 Highways(最小生成树,基础,最大边权)
题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ2395 Out of Hay(求最小生成树中最大的边权,Kruskal)
POJ2395 Out of Hay 寻找最小生成树中最大的边权. 使用 Kruskal 求解,即求选取的第 \(n-1\) 条合法边. 时间复杂度为 \(O(e\log e)\) . #includ ...
- poj 2485 求最小生成树中 最长的一条边
Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...
随机推荐
- autolayout UIImageView 根据 UILabel的宽度变换位置
仅个人学习笔记,大牛勿喷 代码写法 使用Masonry //昵称 _nameLableView = [[UILabel alloc]init]; [_nameLableView setTextColo ...
- 跟我一起玩Win32开发(21):复制&粘贴&剪贴板操作
我要提醒一下大家,看了我的博文学到的知识,千万不要用于实际开发,不然你会被你的上司骂:“妈的,这些东西哪来的,从来没有人这样做过.”不信你试试,脑细胞被冻结的经理或者技术总监们肯定会这样说的. 如果是 ...
- C++ 的浅拷贝和深拷贝(结构体)
关于浅拷贝和深拷贝这个问题遇上的次数不多,这次遇上整理一下,先说这样一个问题,关于浅拷贝的问题,先从最简单的说起. 假设存在一个结构体: struct Student { string name; i ...
- 清除select自带小三角
select {/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/appeara ...
- AtCoder Grand Contest 003 D - Anticube
题目传送门:https://agc003.contest.atcoder.jp/tasks/agc003_d 题目大意: 给定\(n\)个数\(s_i\),要求从中选出尽可能多的数,满足任意两个数之积 ...
- 利用uiautomator实现Android移动app启动时间的测试
为了减少因手工测试的反应误差,这里介绍下如何利用Android自带的自动化测试工具uiautomator实现app启动时间的测试. 测试基本思路如下: 1.启动前记录当前的时间戳 2.启动app,直至 ...
- 500 Keyboard Row 键盘行
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 详见:https://leetcode.com/problems/keyboard-row/description/ C++: cl ...
- (023) 关于51单片机的A5指令
可看见一篇讲51单片机加密的文章这么讲: [加密方法:在A5后加一个二字节或三字节操作码,因为所有反汇编软件都不会反汇编A5指令,造成正常程序反汇编乱套,执行程序无问题仿制者就不能改变你的源程序.] ...
- solr 包地址
http://archive.apache.org/dist/lucene/solr/6.3.0/
- asp.net core教程 (一)
Asp.Net Core简介 ASP.NET Core 是一个全新的开源.跨平台框架,可以用它来构建基于网络连接的现代云应用程序,比如:Web 应用,IoT(Internet Of Things,物联 ...