poj_2560 Freckles

Freckles

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 5248

Accepted: 2720

题目:http://poj.org/problem?id=2560

Description

In an episode of the Dick VanDyke show, little Richie connects the freckles on his Dad's back to form apicture 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 amountof 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 asequence 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 linefollows; each following line contains two real numbers indicating the (x,y)coordinates of the freckle.

Output

Your program prints a singlereal number to two decimal places: the minimum total length of ink lines thatcan connect all the freckles.

Sample Input

3

1.0 1.0

2.0 2.0

2.0 4.0

Sample Output

3.41

Source

Waterloolocal 2000.09.23

题意:

       输入n个点的坐标位置,求走完全部点的最小距离。

解题思路:

       这是一个求图的最小生成树的问题,权值就是两个点之间的距离,用prin()算法来求解——先以一个节点(1)作为最小生成树的初始节点,然后在一次选择与这点相连权值最小的边放在集合中,这样不断的向集合中增加节点,直到每个节点添加完成

代码:

#include <iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#define MAX 103

#define VALUE 0xffffff

using namespace std;

struct coor

{

   float x;

   float y;

};

coor cor[MAX];

float g[MAX][MAX];

bool used[MAX];

float d[MAX];

int ts;

 

float distinct(coor x1,coor x2)

{

   return sqrt((x2.x-x1.x)*(x2.x-x1.x)+(x2.y-x1.y)*(x2.y-x1.y));

}

 

float prim()

{

   int i;

   for(i=0;i<=ts;i++)

    {

       used[i]=false;

       d[i]=VALUE;

    }

   d[1]=0;

   float res=0;

   while(true)

    {

       int t=-1;

       for(i=1;i<=ts;i++)

       {

           if(!used[i] && (t==-1 || d[i]<d[t]))

                t=i;

       }

       if(t==-1)

           break;

       used[t]=true;

       res+=d[t];

       for(i=0;i<=ts;i++)

       {

           if(d[i]>g[t][i])

                d[i]=g[t][i];

       }

    }

   return res;

}

 

 

int main()

{

 

    scanf("%d",&ts);

   //建图

   memset(g,0,sizeof(g));

   for(int i=1;i<=ts;i++)

    {

       scanf("%f%f",&cor[i].x,&cor[i].y);

       for(int j=1;j<i;j++)

       {

           g[i][j]=distinct(cor[i],cor[j]);

           g[j][i]=g[i][j];

       }

 

    }

   printf("%.2f\n",prim());

   return 0;

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值