POJ 2485 Highways( 最小生成树)
Description
The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publichighways. So the traffic is difficult in Flatopia. The Flatopian government isaware of this problem. They're planning to build some highways so that it willbe possible to drive between any pair of towns without leaving the highwaysystem.
Flatopian towns are numbered from 1 to N. Each highway connects exactly twotowns. All highways follow straight lines. All highways can be used in bothdirections. Highways can freely cross each other, but a driver can only switchbetween 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 tobe built. However, they want to guarantee that every town is highway-reachablefrom every other town.
Input
The firstline 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 thenumber 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 aninteger within [1, 65536]) between village i and village j. There is an emptyline after each test case.
Output
For eachtest case, you should output a line contains an integer, which is the length ofthe longest road to be built such that all the villages are connected, and thisvalue is minimum.
Sample Input
1
3
0 990 692
990 0 179
692 179 0
Sample Output
692
分析:
最小生成树问题,但不是求边的权值之和,而是求最小生成树中最长的那条边的长度。用Prim算法和Kruskal算法均可以。由于题目的测试数据是以邻接矩阵的形式给出的,因此可以考虑优先使用Prim算法。如果使用Kruskal算法,还需要把邻接矩阵中的顶点和边分离出来。
prim代码:
#include<stdio.h>
#include<iostream>
using namespace std;
int n;
int tu[505][505];
int dis[505],vis[505];
void prim()
{
for(int i=1;i<=n;i++)//初始化,所有点的距离以及是否访问过
{
dis[i]=tu[1][i];
vis[i]=0;
}
vis[1]=1;//标记1号顶点已经访问过
int Min;
int k;
int ans=0;//保存最小生成树里面的最大边
//int sun=0;//最小生成树的值
for(int i=1;i<n;i++)
{
Min=0x3f3f3f3f;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&Min>dis[j])
{
Min=dis[j];
k=j;
}
}
if(ans<dis[k])
ans=dis[k];
vis[k]=1;//标记k这个点已经访问过
//sum+=dis[k];
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&dis[j]>tu[k][j])
dis[j]=tu[k][j];
}
}
printf("%d\n",ans);
//printf("%d\n",sum);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&tu[i][j]);
prim();
}
}
Kruskal算法:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int Count;
int tu[505][505];
int Tree[505];//并查集的数组
struct Node
{
int a,b,w;
} node[505*505];
bool cmp(Node A,Node B)
{
if(A.w!=B.w)
return A.w<B.w;
if(A.a!=B.a)
return A.a<B.a;
return A.b<B.b;
}
int findRoot(int x) //递归查找顶点x所在树的根
{
if (Tree[x] == x) return x;//若为x,则x的根就是自身
else //否则
{
int tmp =findRoot(Tree[x]);//递归查找x的父亲Tree[x]的根,tmp为最终的树根
Tree[x]= tmp; //查找过程中进行路径压缩:把x到根之间遇到的所有顶点的父亲设为tmp
return tmp; //返回树根
}
}
void Kruskal()
{
int ans=0;
int k=0;
int sum=0;
for(int i=0; i<Count; i++)//遍历排序好的每一条边
{
if(k==n-1) break;//如果当前的生成树中已经有了n-1条边,就不用在接着往下找了
int a=findRoot(node[i].a);
int b=findRoot(node[i].b);
if(a!=b)
{
k++;
Tree[a]=b;
//sum+=node[i].w;
if(ans<node[i].w)
ans=node[i].w;
}
}
printf("%d\n",ans);
//printf("%d\n",sum);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
scanf("%d",&tu[i][j]);
}
for(int i=1; i<=n; i++)
Tree[i]=i;//标记没一个点都是只属于自身的集合
Count=0;
for(int i=1; i<=n; i++)
for(int j=1; j<i; j++)
{
node[Count].a=i;
node[Count].b=j;
node[Count].w=tu[i][j];
Count++;
}
sort(node,node+Count,cmp);
Kruskal();
}
return 0;
}
POJ 2485 Highways( 最小生成树)的更多相关文章
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- 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 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 ...
随机推荐
- jQuery 版本选择与常见插件库总结
在日常的开发中jQuery作为一个流行多年的轻量级 JavaScript 库,使用十分的普遍,主要源于它的便捷性和实用性非常高. 在此总结一些关于jQuery版本的区别和选择的建议,以及一些常见插件库 ...
- MacOS & dock 工具栏 & 外接显示器 & 主屏
MacOS & dock 工具栏 & 外接显示器 & 主屏 macos 如何将 dock工具栏从外接显示器拖回主屏 https://support.apple.com/zh-c ...
- SQL查找删除重复行
本文讲述如何查找数据库里重复的行.这是初学者十分普遍遇到的问题.方法也很简单.这个问题还可以有其他演变,例如,如何查找“两字段重复的行”(#mysql IRC 频道问到的问题) 如何查找重复行 第一步 ...
- BZOJ3289 Mato的文件管理(莫队+树状数组)
这个做法非常显然. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...
- ReentrantLock详解 以及与synchronized的区别
ReentrantLock lock = new ReentrantLock(); //参数默认false,不公平锁 ReentrantLock lock = new ReentrantLock(tr ...
- 【刷题】LOJ 2480 「CEOI2017」One-Way Streets
题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,现在想要把这张图定向. 有 \(p\) 个限制条件,每个条件形如 \((xi,yi)\) ,表示在新的有向图当中,\(x_i\) 要能够 ...
- 单点登录(五)-----遇到问题-----cas server 源码部署tomcat运行报错BeanCreationException:Error creating bean with name 's
我们在上一篇文章已经解决了把下载好的cas server源码部署到tomcat缺少子项目编辑文件或者jar包导致找不到class报错的问题 单点登录(四)-----遇到问题-----cas serve ...
- 解题:NOI 1999 生日蛋糕
题面 裸的搜索题,就说剪枝(注:nw->noww->当前,res->rest->剩余): 1.想达到$Nπ$的体积,那么半径一开始最多也就$sqrt(n)$了,再大就超了... ...
- 音视频处理之FFmpeg+SDL视频播放器20180409
一.FFmpeg视频解码器 1.视频解码知识 1).纯净的视频解码流程 压缩编码数据->像素数据. 例如解码H.264,就是“H.264码流->YUV”. 2).一般的视频解码流程 视频码 ...
- 关于nodejs的几个干货(读中文文件编码问题/发送邮件/定时任务)
关于nodejs读取中文文件真是折腾了不少时间,网上各种方案,最后没有一个适用我,好在解决了. 下面的三个知识点都是从项目中抽出的,要单独运行脚本的话需要用全局模式来安装模块,比如安装中文转换模块(后 ...