最小生成树与并查集
来源:互联网 发布:爱普生l455清零软件 编辑:程序博客网 时间:2024/05/17 04:01
Description
In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
Input
There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.
Output
For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
Sample Input
42 30 01 00 -1 1 -10
Sample Output
3.41题意:给出几个点的坐标,某些点必须直接连接,求最小生成树。思路:把需要的点连接起来,把并查集中这些点的前驱赋值即可,然后用克鲁斯卡尔算法,把这些距离从小到大排序,每次取最小的点,用并查集使其不能成环。#include <iostream>#include<stdio.h>#include<math.h>#include<algorithm>#define MAX 55using namespace std;int pre[MAX];//并查集数组int X[MAX],Y[MAX];//存储每个<span style="font-family:Arial;">点的横纵坐标</span>struct Node{ int x,y;//表示两个不同的点 double w;//这两个点的距离}sun[MAX*MAX];double length(int a,int b)//求距离{ return sqrt((X[a]-X[b])*(X[a]-X[b])+(Y[a]-Y[b])*(Y[a]-Y[b]));}int cmp(const Node a,const Node b)//把距离从小到大排序{ return a.w<b.w;}int unionsearch(int root)//并查集{ int son,tmp; son=root; while(root!=pre[root]) root=pre[root]; while(son!=root)<span style="font-family:Arial;">//并查集</span> { tmp=pre[son]; pre[son]=root; son=tmp; } return root;}int main(){ int N; while(scanf("%d",&N)!=EOF&&N) { int p,q; double sum; int l=0; scanf("%d%d",&p,&q); for(int i=1;i<=N;i++){ scanf("%d%d",&X[i],&Y[i]); } sum=length(p,q); for(int k=1;k<=N;k++) { for(int w=1;w<=N;w++) sun[l++]=Node{k,w,length(k,w)};//把<span style="font-family:Arial;">k到w的距离以及k,w,存在sun数组中</span> } sort(sun,sun+l,cmp); for(int u=1;u<=N;u++) pre[u]=u; pre[p]=q;//p,q两点已经连接 for(int e=0;e<l;e++) { int c=unionsearch(sun[e].x); int v=unionsearch(sun[e].y); if(c!=v)<span style="font-family:Arial;">//未成环</span> { pre[c]=v; sum+=sun[e].w; } } printf("%.2lf\n",sum);//保留两位小数 } return 0;}
0 0
- 并查集与最小生成树
- 最小生成树与并查集
- 最小生成树与并查集
- 并查集与kruskal最小生成树
- HDU1863 并查集与最小生成树
- 并查集模版&最小生成树
- 最小生成树kruscal+并查集
- 并查集+最小生成树_HDU_1102
- bzoj1050(最小生成树 并查集)
- 最小生成树+并查集+ kruskal
- 并查集 最小生成树
- 最小生成树(并查集)
- 最小生成树+并查集模板
- 最小生成树&并查集练习
- 并查集/最小生成树
- 并查集 +最小生成树
- 最小生成树模板 并查集
- 最小生成树(并查集)
- solr
- 绕过安全狗与360PHP一句话的编写
- 案例分析企业网站基本SEO优化修改建议
- ION 内存管理
- iOS_CollectionView,scrollToItemAtIndexPath方法失效。
- 最小生成树与并查集
- 安卓实时弹幕demo(三)消息透传服务器
- Java设计模式-代理模式
- TCP的流量控制和拥塞控制
- Delphi配置文件之单例模式
- SSL证书与Https应用部署小结
- 怎么把小丑的可怕和疯狂表现出来
- Intellij IDEA 15.0.2 热部署配置
- union的长度