HDU 1102 Constructing Roads(最小生成树 Kruskal算法)
来源:互联网 发布:java redis list 存取 编辑:程序博客网 时间:2024/05/05 07:21
原题
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), 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, 1000]) between village i and village j.Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.Sample Input
30 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
题意
输入一个邻接矩阵来表示无向图,再输入一些路径表示已连通,你要再加入一些最短的路径路径使得该图为连通图,输出这些路径的长度和。
涉及知识及算法
求最小生成树的Kruskal算法(加边法),用并查集来判断是否有通路,利用#<algorithm>的sort函数对边进行排序。
代码
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAX_N=110;const int MAX_Q=MAX_N*MAX_N;int N,Q;int p[MAX_N];struct node{ int u,v,w;};node edge[MAX_Q];bool cmp(node a,node b){ return a.w<=b.w;}int Find(int x){ return p[x]==x?x:Find(p[x]);}int Kruskal(int m){ int ans=0; int i,x,y; //对边进行排序 sort(edge,edge+m,cmp); for(i=0;i<m;i++) { x=edge[i].u; y=edge[i].v; x=Find(x); y=Find(y); //如果x和y之间没有通路 if(x!=y) { p[x]=y; ans+=edge[i].w; } } return ans;}int main(){ int k,dis,a,b; while(cin>>N) { memset(edge,0,sizeof(edge)); k=0; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { cin>>dis; //只处理上三角矩阵 if(i>=j) { continue; } edge[k].u=i; edge[k].v=j; edge[k].w=dis; k++; } } //并查集的初始化 for(int i=0;i<N;i++) { p[i]=i; } cin>>Q; for(int i=0;i<Q;i++) { cin>>a>>b; a--,b--; //加入为一个集合(一条边为一个集合) a=Find(a); b=Find(b); p[a]=b; } cout<<Kruskal(k)<<endl; } return 0;}代码转载自HDUOJ用户csc12,向他表示感谢。
阅读全文
0 0
- HDU 1102 Constructing Roads(最小生成树 Kruskal算法)
- HDU 1102 Constructing Roads 最小生成树-Kruskal算法
- hdu-1102-Constructing Roads-最小生成树,Kruskal算法
- HDU 1102 Constructing Roads (最小生成树+Kruskal算法入门)
- hdu hdu 1102 Constructing Roads(最小生成树Kruskal)
- 【最小生成树 kruskal】 hdu 1102 Constructing Roads
- hdu 1102 Constructing Roads 最小生成树Kruskal
- HDU 1102 Constructing Roads (最小生成树,kruskal)
- poj-2421 Constructing Roads(最小生成树 Kruskal算法)
- HDU 1102 Constructing Roads【简单最小生成树,Prime算法+Kruskal算法】
- Constructing Roads (最小生成树 Kruskal)
- HDU - 1102 - Constructing Roads (最小生成树--prim算法!!)
- HDU 1102 Constructing Roads (最小生成树 Prim算法)
- HDU 1102 Constructing Roads(Kruskal最小生成树求最小花费)
- hdu 1102 Constructing Roads(Kruskal算法)
- 杭电oj-1102 Constructing Roads(最小生成树Kruskal算法)
- poj 2421 Constructing Roads(最小生成树 kruskal算法)
- POJ 2421 Constructing Roads 最小生成树 Kruskal算法
- maven 依赖的jar包打包问题
- Struts2知识点回顾(四)怎么传递对象参数?
- Java 线程
- Hibernate简单的拦截器原理
- laravel event事件 讲解
- HDU 1102 Constructing Roads(最小生成树 Kruskal算法)
- php 生成唯一订单号4种方法
- Python文件操作
- Failed to execute goal on project...
- shouldChangeCharactersInRange方法不执行
- C#仿微信飞机大战_<零度屏幕录像软件>改版
- 利用github参与项目
- 学前段半个月的一些心得(二)
- 由bootstrap-fileinput.js图片预览高度BUG谈谈如何提高驾驭开源插件的能力