最小生成树与并查集

来源:互联网 发布:爱普生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.

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
原创粉丝点击