Freckles

链接:https://www.nowcoder.com/questionTerminal/41b14b4cd0e5448fb071743e504063cf
来源:牛客网
 

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

题目解析:题目给出了点的个数以及各点的坐标,要求我们求解将各点连接起来的最短长度。我们可以将这些点当成一个无向图中的各个节点,而图的边长则为节点之间的距离。这样我们就可以将题目的求解转换成无向图的最小生成树的个边的和的长度。显然我们可以采用prim算法或者克鲁斯卡尔算法。因为本题的无向图为一个完全图,因此根据两种算法的时间复杂度,我们选择时间复杂度小的prim算法。

代码实现:

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>

using namespace std;

#define N 110
#define Max 999999

struct Node{
    double x;
    double y;
};//用于存储点的坐标

Node node[N];//用于存储坐标点
double Edge[N][N];//用于存储各点之间的距离
bool flog[N];//用于判断该点是否已经加入最小生成树的节点中
int tree[N];//用于存储最小生成树中的节点
int count;//记录最小生成树中节点的个数

int main()
{
    int n;
    while(~scanf("%d",&n))//输入节点个数
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&node[i].x,&node[i].y);//输入各坐标点
        }
        for(int i=0;i<n;i++)
        {
            flog[i]=false;
            Edge[i][i]=0;
            for(int j=i+1;j<n;j++)
            {
                Edge[i][j]=sqrt(pow((node[i].x-node[j].x),2)+pow((node[i].y-node[j].y),2));
                Edge[j][i]=Edge[i][j];//构造无向图,边的长度为各点之间的距离
            }
        }
        count=1;
        tree[0]=0;
        flog[0]=true;//将节点一加入到最小生成树的节点集合中
        double min_edge;
        double sum=0;
        int k;
        while(count<n)
        {
            min_edge=Max;
            for(int i=0;i<count;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(Edge[tree[i]][j]<min_edge&&flog[j]==false)
                    {
                        min_edge=Edge[tree[i]][j];
                        k=j;
                    }
                }
            }//寻找最小生成树中节点到其它节点最小的边。
            tree[count++]=k;//将该节点加入到最小生成树节点集合
            flog[k]=true;
            sum=sum+min_edge;//将该边的长度加入到sum中
        }
        printf("%.2lf\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值