POJ 2485 Highways (水题入门最小生成树)
来源:互联网 发布:什么是网络渗透技术 编辑:程序博客网 时间:2024/06/10 09:57
Description
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways 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 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 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
For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
Sample Input
1
3
0 990 692
990 0 179
692 179 0
Sample Output
692
题目大意
在 1~n 个城镇之间建设公路,求可以把所有城镇连接起来的最短路。
解题思路
1. Prim算法: Prim算法是选取一个点作为出发点,在其余点中选取距离该点最近的顶点,与出发点合并为集合,并标记已经添加过的顶点避免重复访问;之后不断往集合中添加距离现有生成树最近的点,扩大现有生成树集合,直至所有的点均被标记,现有生成树集合不再被更新,退出算法。
2.Kruskal算法:Kruskal算法是先把边递增排序,然后由小边至大边进行检索,若加入该点会成环,那么舍弃;否则现有生成树存在顶点数 +1,直至顶点数等于图所有顶点,退出算法。在该算法中,使用并查集来判断两点是否属于同一集合,即是否存在环。
代码实现
//Prim算法
#include <iostream>#include<cstdio>using namespace std;#define maxv 506#define INF (1<<26)int edge[maxv][maxv],n;void prim(){ int mincost[maxv]; bool flag[maxv]; for(int i=0;i<n;i++) { mincost[i]=INF; flag[i]=false; } mincost[0]=true; int res=0; while(true) { int v=-1; for(int u=0;u<n;u++) { if(!flag[u]&&(v==-1||mincost[u]<mincost[v])) v=u; } if(v==-1)break; flag[v]=true; res=max(res,mincost[v]); for(int u=0;u<n;u++) mincost[u]=min(mincost[u],edge[u][v]); } printf("%d\n",res);}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d",&edge[i][j]); prim(); } return 0;}
//kruskal算法
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define maxn 506int n;int pre[maxn];bool flag[maxn];struct node{ int x,y,l;} edge[maxn*maxn];bool cmp(node a,node b){ return a.l<b.l;}int find_pre(int x){ int r=x; while(pre[r]!=r) { r=pre[r]; } return r;}bool make_union(int a,int b){ int fx=find_pre(a); int fy=find_pre(b); if(fx!=fy) { pre[fx]=fy; return true; } return false;}int main(){ int T,v,len; scanf("%d",&T); while(T--) { memset(flag,false,sizeof(flag)); scanf("%d",&n); v=0; for(int i=0; i<n; i++) { pre[i]=i; for(int j=0; j<n; j++) { scanf("%d",&len); if(i>j) { edge[v].x=i; edge[v].y=j; edge[v].l=len; v++; } } } int nowv=0; int maxx=0; sort(edge,edge+v,cmp); for(int i=0; i<v; i++) { if(make_union(edge[i].x,edge[i].y)) { nowv++; if(maxx<edge[i].l) maxx=edge[i].l; if(nowv==v-1) break; } } printf("%d\n",maxx); } return 0;}
- POJ 2485 Highways (水题入门最小生成树)
- POJ 2485 Highways【最小生成树】
- poj 2485Highways(最小生成树 Kruskal)
- poj 2485 Highways (最小生成树)
- poj 2485 Highways 最小生成树
- poj 2485 Highways prim最小生成树
- POJ 2485 Highways (最小生成树)
- POJ 2485 Highways 最小生成树
- POJ 2485 Highways(最小生成树)
- poj 2485 Highways (最小生成树)
- POJ 2485 Highways 最小生成树
- poj 2485 Highways 最小生成树
- POJ 2485 Highways (最小生成树)
- POJ 2485 Highways (prim最小生成树)
- poj-2485-Highways-最小生成树
- POJ 2485 Highways(最小生成树)
- POJ 2485 Highways【最小生成树】
- POJ 2485 Highways 最小生成树
- 数组的多种写法
- 浏览器缓存详解:expires,cache-control,last-modified,etag详细说明
- 运用SQLite存储数据
- JRE和JDK的区别
- Learn PHP第一章:基础语句2(安装PHP)
- POJ 2485 Highways (水题入门最小生成树)
- android 解析任意格式的json字符串
- Class org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe not found
- opencv的HoughCircles( )函数
- Bootstrap笔记4—表单
- 任意大小的图片在固定容器居中显示
- Spark性能优化之道——解决Spark数据倾斜(Data Skew)的N种姿势
- ant编译Android项目的过程
- android 计时器