ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++最小生成树(克鲁斯卡尔)——Freckles

2021-02-17 15:32:11  阅读:168  来源: 互联网

标签:int Freckles 克鲁斯 C++ double freckles include lines Richie


题目描述

    In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.      Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

输入描述:

    The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出描述:

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

示例1

输入

3
1.0 1.0
2.0 2.0
2.0 4.0

输出

3.41

思路

本题和“还是畅通工程”的区别在于,本题是输入所有的点的坐标,而不是所有的无向边。

所以将所有的点进行连线,然后用并查集进行克鲁斯卡尔算法的计算即可。

需要注意的是,在map中要利用自己定义的结构体作为关键字,必须要进行“<”字符的重载

 

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
const int MAXN=100;
struct Point{
    double x;
    double y;
    int priority;
    bool operator!=(const Point& p) const{
        return x!=p.x||y!=p.y;
    }
    bool operator<(const Point& p) const{ //为了使map能用自定义的结构体作为关键字
        return priority<p.priority;
    }
};
struct Edge{
    Point from;//边的起点
    Point to;//边的终点
    double length;//边的长度
    bool operator<(const Edge& e) const{
        return length<e.length;
    }
};
Edge edge[MAXN*MAXN];//边集
Point point[MAXN];//点集
map<Point,Point> father;
map<Point,int> height;
void Initial(int n)
{
    for(int i=0;i<=n;i++)
    {
        father[point[i]]=point[i];
        height[point[i]]=0;
    }
}
Point Find(Point p)
{
    if(p!=father[p])
        father[p]=Find(father[p]);
    return father[p];
}
void Union(Point x,Point y)
{
    x=Find(x);
    y=Find(y);
    if(x!=y)
    {
        if(height[x]<height[y])
            father[x]=y;
        else if(height[x]>height[y])
            father[y]=x;
        else{
            father[y]=x;
            height[x]++;
        }
    }
}
double Kruskal(int n,int edgeNumber)
{
    Initial(n);
    sort(edge,edge+edgeNumber);//按权值排序
    double sum=0.0;
    
    for(int i=0;i<edgeNumber;i++)
    {
        Edge current=edge[i];
        if(Find(current.from)!=Find(current.to))
        {
            Union(current.from,current.to);
            sum+=current.length;
        }
    }
    return sum;
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)
            break;
        int edgeNumber=n*(n-1);
        for(int i=0;i<n;i++)
        {
            cin>>point[i].x>>point[i].y;
            point[i].priority=i;
        }
        int m=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i!=j)
                {
                    edge[m].from=point[i];
                    edge[m].to=point[j];
                    double num=(point[i].x-point[j].x)*(point[i].x-point[j].x)
                                    +(point[i].y-point[j].y)*(point[i].y-point[j].y);
                    edge[m].length=sqrt(num);
                    m++;
                }
            }
        }
        double answer=Kruskal(n,edgeNumber);
        printf("%.2f\n",answer);
    }
    return 0;
}

 

标签:int,Freckles,克鲁斯,C++,double,freckles,include,lines,Richie
来源: https://blog.csdn.net/qq_36935691/article/details/113828385

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有