POJ 2485 Highways 最小生成树 (Kruskal)
Description
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 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 题意: 先输入T,有T组数据。再输入有N个点。N行N列 每一个数据代表I点到J点的距离。求完毕一个 最小生成树种 的 最大边。#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define q 505
int fa[q];
int map[q][q];
struct node{
int x,y,l;
}q[q*q/2];
int find(int x)
{
return x==fa[x]? x:find(fa[x]);
}
int cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
int k,t,n,i,j,max1;
scanf("%d",&t);
while(t--)
{
max1=0;
k=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fa[i]=i;
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(i<j) // 优化:去掉重边还有中间的0
{
k++; //不知道有多少边。这样统计
q[k].x=i;
q[k].y=j;
q[k].l=map[i][j];
}
}
}
sort(q+1,q+1+k,cmp); //依据每条边的距离排序
for(i=1;i<=k;i++)
{
if(fa[find(q[i].x)]!=fa[find(q[i].y)]) //并查集思想。。 {
fa[find(q[i].x)]=find(q[i].y);
if(q[i].l>max1)
max1=q[i].l;
}
}
cout<<max1<<endl;
}
return 0;
}Kruskal 模板。。
POJ 2485 Highways 最小生成树 (Kruskal)的更多相关文章
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- poj 2485 Highways(最小生成树,基础,最大边权)
题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...
随机推荐
- Sql 存储过程动态添加where条件
)= '2,3' )= '' ) if(@bussHallId is not null) set @strWhere = @strWhere + ' and bh.ID in ('+@bussHall ...
- [转] Redis在windows下安装过程
转载自(http://www.cnblogs.com/M-LittleBird/p/5902850.html) 一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的win ...
- 生成Nuget 源代码包来重用你的Asp.net MVC代码
ASP.NET 开发人员有时会陷入一种困境:想要重用以前写过的东西,如一些具有完整功能的Web页面+后台逻辑, 往往不那么直接了当,因此很不爽.经常采用的方式是:找到以前写过的项目,从中挑出来一些有用 ...
- Git——github基本操作
基本概念 上一篇文章写到git共享仓库,但是有个局限性,就是这个仓库存在于本地,其他人无法从我们这个仓库拿到共享的内容 但是我们可以将这个共享仓库放入一个远程的服务器上,然后设置一些登录权限就能完美的 ...
- discuz x3论坛搬家换虚拟主机完美使用教程 亲测可行 附操作步骤
第一步:备份网站数据进入后台—站长—数据库—备份,数据备份类型选择“Discuz!和 UCenter数据”,备份成功以后,数据自动保存在data文件夹下. 第二步:网站文件下载 把整个网站文件打包(虚 ...
- python实现二叉树的遍历以及基本操作
主要内容: 二叉树遍历(先序.中序.后序.宽度优先遍历)的迭代实现和递归实现: 二叉树的深度,二叉树到叶子节点的所有路径: 首先,先定义二叉树类(python3),代码如下: class TreeNo ...
- java中随机生成字符串的方法(三种)
org.apache.commons.lang(2.6): 链接:https://pan.baidu.com/s/1k_oeA5AjSt6evoR7zT8gpQ 提取码:yhl5 1.生成的字符串每个 ...
- python bs4库
Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...
- 【Redis】四、Redis设计原理及相关问题
(六)Redis设计原理及相关问题 通过前面关于Redis五种数据类型.相关高级特性以及一些简单示例的使用,对Redis的使用和主要的用途应该有所掌握,但是还有一些原理性的问题我们在本部分做一个探 ...
- Python使用Flask框架,结合Highchart处理xml数据
1.html代码 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...