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;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章