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 ...
随机推荐
- Centos 6.x 搭建 Zabbix Agent 客户端
如需搭建zabbix server端,请参考:Zabbix-Server配置 环境: Zabbix-Server: Centos 6.8 IP:192.168.126.129 #Zabix- ...
- Folding UVA - 1630
题目 ans[i][j]表示由原串第i个字符到第j个字符组成的子串的最短折叠长度如果从i到j本身可以折叠,长度就是本身长度或折叠后的长度的最小值***此处参考:http://blog.csdn.net ...
- 18.5.1使用Proxy和InvocationHandler创建动态代理
package d18_5_1; public interface Person { void walk(); void sayHello(String name); } package d18_5_ ...
- 容器API
- Git之fatal: remote origin already exists
文件提交到远程分支,我们需要提前表明需要提交到哪个远程分支 比如:git remote add origin git@github.com:wqk66/test.git,表示他提交到远程仓库test ...
- 微信小程序 图片加载失败处理方案
小程序端展示网络资源图片可能会失败,下面介绍一种自己的处理方法 1. js文件中判断图片 url 是否存在,存在则正常显示,不存在则替换url为本地默认图片 2. 当图片 url 存在,但是加载失败时 ...
- ES之基本数据类型之间的显示转换和隐式转换
typeof(数据)/ typeof 数据 判断数据的数据类型,typeof返回的都是字符串 输出结果类型有:number.string.boolean. undefined.object.funct ...
- HttpURLConnection读取http信息
废话不多说,直接上code. package mytest; import java.io.BufferedReader; import java.io.IOException; import jav ...
- 分享一个开源的JavaScript统计图表库,40行代码实现专业统计图表
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- 事件冒泡 & 阻止事件冒泡
事件冒泡 : 当一个元素接收到事件的时候,会把他接收到的所有传播给他的父级,一直到顶层window.事件冒泡机制 阻止冒泡 : 当前要阻止冒泡的事件函数中调用 event.cancelBubble = ...