复习最小生成树问题
来源:互联网 发布:农业普查数据 编辑:程序博客网 时间:2024/06/04 23:35
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">忘得差不多了,回忆下kruskal求最小生成树算法,首先得到边信息,对边按权值大小排序,每次选择最小且不在一个联通分量的边,直到连成一棵最小生成树。其中要用上并查集的知识。</span>
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.
Input contains multiple test cases. Process to the end of file.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
31.0 1.02.0 2.02.0 4.0
Sample Output
3.41
<span style="font-size:24px;">#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn=105;const int maxm=maxn*maxn/2;struct Point{ double x,y;};struct Edge{ int u,v; double w;};Point p[maxn];Edge e[maxm];int uset[maxn];int cmp(Edge x,Edge y){ return x.w<y.w;}int Find(int x) //并查集的查找函数{ if(x==uset[x]) return x; else return uset[x]=Find(uset[x]);}int main(){ int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } int nEdge=0; for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { e[nEdge].u=i; e[nEdge].v=j; double temp=(p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y); e[nEdge++].w=sqrt(temp); } } sort(e,e+nEdge,cmp); //对边排序 double sum=0; for(int i=0;i<n;i++) uset[i]=i; for(int i=0;i<nEdge;i++) { int u=Find(e[i].u); int v=Find(e[i].v); double w=e[i].w; if(u!=v) //如果不在一个连通分量,合并 { uset[u]=v; sum+=w; } } printf("%.2lf\n",sum); } return 0;}</span>
0 0
- 复习最小生成树问题
- [复习]最小生成树 最优布线问题
- 【最小生成树】图论复习(三)
- soj 3366 复习最小生成树kruskal
- POJ2421——最小生成树复习
- 最后复习之最小生成树
- 蒟蒻复习之-----最小生成树
- 【复习记录】最小/大生成树
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- 最小生成树问题
- Qt中多线程的一种使用
- 文件帮助类
- 为自己立言!
- VBS实现文件逐字节异或
- view,SurfaceView,GLSurfaceView的关系和区别
- 复习最小生成树问题
- Android清除本地数据缓存代码
- linux
- java : 包装类 缓冲机制的使用
- [notes]chap14 Iterator Generator-expression 生成器表达式
- 博弈论小结
- Netty中的ByteBuf原理分析
- [DP 容斥原理] BZOJ3622 已经没有什么好害怕的了 && BZOJ 2024 [SHOI2009] 舞会
- 项目1:个人所得税计算器