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 ...
随机推荐
- SQL中进行转列的几种方式
SQL中进行转列 在很多笔试的程序员中会有很多写SQL的情况,其中很多时候会考察行转列.那么这个时候如果能写出来几种行转列的SQL,会给面试官留下比较好的印象. 以下是这次sql转换的表结构以及数据 ...
- 搜狗输入法ubuntu
1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...
- iOS 在UITextView中查找某个Range所在的Rect
代码如下(Swift 4): extension UITextView { /// 查找文本范围所在的矩形范围 /// /// - Parameter range: 文本范围 /// - Return ...
- Centos 搭建Mysql-Proxy 读写分离
Mysql 读写分离 主:192.168.153.130 从:193.168.153.131 Mysql-Proxy:193.168.153.132 这里省略mysql主从同步,如果有需要,请查看:M ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E
Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...
- 转 sql 查出一张表中重复的所有记录数据
select * from DB_PATCH awhere lower(a.db_name) in (select lower(db_name) from DB_PATCH group by lowe ...
- Appium + python自动化 - 启动app
各种百度将Appium + python的环境搭建好后,开启移动app自动化的探索(基于Android),首先来记录下如何启动待测的app吧! 如何启动app呢?首先要获取包名,然后获取launche ...
- 520 Detect Capital 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如&q ...
- 496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...
- 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering
http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...