Freckles(九度 OJ 1144)

Freckles(九度 OJ 1144)

时间限制:1 秒 内存限制:128 兆 特殊判题:否

1.题目描述:

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.
样例输入:
3
1.0 1.0
2.0 2.0
2.0 4.0
样例输出:
3.41

2.基本思路
该题的大致意思为平面上有n个点,而且给出了每一个点的坐标(x,y)。求将这些点都连接起来所需的线的最短长度。咋一看,其实就是最小生成树的问题。这里同样采用基于并查集实现的Kruskal算法来进行求解。首先根据提供的n个点,计算两两之间的距离得到对应的边集。然后对边集中的边根据权重进行升序排序。遍历每一条边,如果当前边的连接的两个结点已经在同一个集合当中,那么继续遍历下一条边,否则将两个结点所在的集合进行合并,并累加当前的权重到最终的答案中,以此反复直到处理完边集中所有的边为止。

3.代码实现

#include <iostream>
#include <algorithm>
#include <math.h>
#define N 101

using namespace std;
struct Point{
    double x;
    double y;

}point[N];

struct Edge{
    int p1;
    int p2;
    double cost;
    operator < (const Edge&A)const{
        return cost<A.cost;
    }
}buf[N*(N-1)/2];

int Tree[N];
int n;


int findRoot(int x){
    int tmp;
    if(Tree[x]==-1){
        return x;//x为集合的根结点
    }
    else{
        tmp = findRoot(Tree[x]);
        Tree[x] = tmp;//每次查询的时候,把查询到的父节点不是根结点的结点的父节点都置为根结点
                      //路径压缩,防止并查集树退化为线性结构。
    }
    return tmp;
}

int main()
{
    double ans=0;
    int r1,r2;
    int cnt=1;//对边进行计数
    while(scanf("%d",&n)!=EOF&&n!=0){
        ans=0;
        cnt=1;
        for(int i=1;i<=n;i++){//输入所有的点
            scanf("%lf%lf",&point[i].x,&point[i].y);
        }

        for(int i=1;i<=n;i++){
            Tree[i] = -1;
        }

        for(int i=1;i<=n;i++){//遍历所有的结点,构造两两点之间的边集
            for(int j=i+1;j<=n;j++){
                if(i!=j){
                    buf[cnt].p1 = i;
                    buf[cnt].p2 = j;
                    buf[cnt].cost = sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y));
                    cnt++;
                }
            }
        }

        sort(buf,buf+cnt);

        for(int i=1;i<=n*(n-1)/2;i++){
            r1 = findRoot(buf[i].p1);
            r2 = findRoot(buf[i].p2);
//            printf("r1 is %d,r2 is %d\n",r1,r2);
            printf("the buf[i].cost=%f\n",buf[i].cost);
            if(r1!=r2){
                Tree[r1]=r2;//合并两个集合
                ans += buf[i].cost;
            }
        }

        printf("%.2f\n",ans);
    }
    return 0;
}
/*
3
1.0 1.0
2.0 2.0
2.0 4.0
*/

遇到的一些problem
1>.关于printf函数的占位符,对于double类型的值a=1.23234,采用printf("%lf\n",a)输出的结果0.00000?为什么?必须要采用printf("%f\n",a)才可以输出正常结果
对于printf()函数,double和float类型的数值占位符均采用"%f"不能采用"%lf"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值