Step6.1.2 hdu1162Eddy's picture(克鲁斯卡尔算法)

Step6.1.2 hdu1162Eddy's picture

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5966    Accepted Submission(s): 3007

 

 

Problem Description

Eddy begins to like painting picturesrecently ,he is sure of himself to become a painter.Every day Eddy drawspictures in his small room, and he usually puts out his newest pictures to lethis friends appreciate. but the result it can be imagined, the friends are notinterested in his picture.Eddy feels very puzzled,in order to change allfriends 's view to his technical of painting pictures ,so Eddy creates aproblem for the his friends of you.

Problem descriptions as follows: Given yousome coordinates pionts on a drawing paper, every point links with the ink withthe straight line, causes all points finally to link in the same place. Howmany distants does your duty discover the shortest length which the ink draws?

 

 

Input

The first line contains 0 < n <= 100,the number of point. For each point, a line follows; each following linecontains two real numbers indicating the (x,y) coordinates of the point.

 

Input contains multiple test cases. Processto the end of file.

 

 

Output

Your program prints a single real number totwo decimal places: the minimum total length of ink lines that can connect allthe points.

 

 

Sample Input

3

1.0 1.0

2.0 2.0

2.0 4.0

 

 

Sample Output

3.41

 

 

Author

eddy

题解:

克鲁斯卡尔算法。

源代码:

#include <iostream>

#include <algorithm>

#include <stdio.h>

#include <math.h>

using namespace std;

struct Road

{

   intc1,c2;

   doublevalue;

} rd[10050];

int n;

double a[105][2];

int node[105];

void init()

{

   memset(node,-1,sizeof(node));

}

 

double dis(double x,double y,double x1,double y1)

{

   doublesum = (x-x1)*(x-x1) + (y-y1)*(y-y1);

   sum = sqrt(sum);

   returnsum;

}

 

bool mycmp(const Road &x,const Road &y)

{

   if(x.value< y.value)

     returntrue;

   returnfalse;

}

 

int find_set(int x)

{

   if(node[x]== -1)

     returnx;

   returnnode[x] = find_set(node[x]);

}

 

bool merge(int x,int y)

{

   intr1 = find_set(x);

   intr2 = find_set(y);

 

   if(r1== r2)

     return0;

  

   if(r1< r2)

     node[r2] = r1;

   else

     node[r1] = r2;

   return1;

}

int main()

{

   while(cin>> n)

   {

     init();

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

     {

        cin >> a[i][0] >>a[i][1];

     }

     intnum = 0;

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

        for(int k = i+1;k <= n;k++)

        {

          rd[num].c1 = i;

          rd[num].c2 = k;

          rd[num++].value =dis(a[i][0],a[i][1],a[k][0],a[k][1]);

        }

     sort(rd,rd+num,mycmp);

 

     doublesum = 0;

     intcount = 0;

     for(int i = 0;i < num;i++)

     {

        if(merge(rd[i].c1,rd[i].c2))

        {

          count++;

          sum += rd[i].value;

        }

        if(count== n-1)

          break;

     }

     printf("%.2lf\n",sum);

   }

   return0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值