鱼C论坛

 找回密码
 立即注册
查看: 2133|回复: 1

[技术交流] 026:Freckles

[复制链接]
发表于 2018-2-13 14:18:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Messj 于 2018-6-8 23:40 编辑

Description

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.
Input

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.
Output

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

3
1.0 1.0
2.0 2.0
2.0 4.0

Sample Output

3.41

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-13 14:18:35 | 显示全部楼层
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 101

using namespace std;

int tr[N];
int findroot(int x)
{
        if(tr[x]==-1)
                return x;
        else 
        {
                int tmp=findroot(tr[x]);
                tr[x]=tmp;
                return tmp;
        }
}
struct Edge
{
        int a,b;
        double cost;
        bool operator < (const Edge &A) const
        {
                return cost<A.cost;
        }
}edge[6000];
struct Point
{
        double x,y;
        double getDistance(Point A)
        {
                double tmp=(x-A.x)*(x-A.x)+(y-A.y)*(y-A.y);
                return sqrt(tmp);
        }
}list[N];
int main()
{
        int n;
        while(scanf("%d",&n)!=EOF)
        {
                for(int i=1;i<=n;i++)
                        scanf("%lf%lf",&list[i].x,&list[i].y);
                int size=1;
                for(int i=1;i<n;i++)
                        for(int j=i+1;j<=n;j++)
                        {
                                edge[size].a=i;
                                edge[size].b=j;
                                edge[size].cost=list[i].getDistance(list[j]);
                                size++;
                        }
                sort(edge+1,edge+size);
                for(int i=1;i<=n;i++)
                        tr[i]=-1;
                double ans=0.0;
                for(int i=1;i<size;i++)
                {
                        int a,b;
                        a=findroot(edge[i].a);
                        b=findroot(edge[i].b);
                        if(a!=b)
                        {
                                tr[a]=b;
                                ans+=edge[i].cost;
                        }
                }
                printf("%.2lf\n",ans);
        }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-23 07:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表