鱼C论坛

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

[学习笔记] 026:Freckles

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

马上注册加入鱼C,享用更多服务吧^_^

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

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

本帖被以下淘专辑推荐:

最佳答案
17 
 楼主| 发表于 2018-2-13 14:18:35 | 显示全部楼层
  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. #define N 101

  5. using namespace std;

  6. int tr[N];
  7. int findroot(int x)
  8. {
  9.         if(tr[x]==-1)
  10.                 return x;
  11.         else
  12.         {
  13.                 int tmp=findroot(tr[x]);
  14.                 tr[x]=tmp;
  15.                 return tmp;
  16.         }
  17. }
  18. struct Edge
  19. {
  20.         int a,b;
  21.         double cost;
  22.         bool operator < (const Edge &A) const
  23.         {
  24.                 return cost<A.cost;
  25.         }
  26. }edge[6000];
  27. struct Point
  28. {
  29.         double x,y;
  30.         double getDistance(Point A)
  31.         {
  32.                 double tmp=(x-A.x)*(x-A.x)+(y-A.y)*(y-A.y);
  33.                 return sqrt(tmp);
  34.         }
  35. }list[N];
  36. int main()
  37. {
  38.         int n;
  39.         while(scanf("%d",&n)!=EOF)
  40.         {
  41.                 for(int i=1;i<=n;i++)
  42.                         scanf("%lf%lf",&list[i].x,&list[i].y);
  43.                 int size=1;
  44.                 for(int i=1;i<n;i++)
  45.                         for(int j=i+1;j<=n;j++)
  46.                         {
  47.                                 edge[size].a=i;
  48.                                 edge[size].b=j;
  49.                                 edge[size].cost=list[i].getDistance(list[j]);
  50.                                 size++;
  51.                         }
  52.                 sort(edge+1,edge+size);
  53.                 for(int i=1;i<=n;i++)
  54.                         tr[i]=-1;
  55.                 double ans=0.0;
  56.                 for(int i=1;i<size;i++)
  57.                 {
  58.                         int a,b;
  59.                         a=findroot(edge[i].a);
  60.                         b=findroot(edge[i].b);
  61.                         if(a!=b)
  62.                         {
  63.                                 tr[a]=b;
  64.                                 ans+=edge[i].cost;
  65.                         }
  66.                 }
  67.                 printf("%.2lf\n",ans);
  68.         }
  69.         return 0;
  70. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

小甲鱼强烈推荐上一条 /1 下一条

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号

GMT+8, 2018-8-16 14:35

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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