hdu 1162 Eddy's picture(kruskal)

Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many 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 line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.
 

 

Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
 

 

Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
 

 

Sample Output
3.41
 
 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 struct scn1
 6 {
 7     int a,b;
 8     double c;
 9 }scn[5005];
10 double sx[110],sy[110];
11 double distance(double x1,double y1,double x2,double y2)
12 {
13     return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
14 }
15 int comp(scn1 a,scn1 b){return a.c<b.c;}
16 int parent[5005];
17 int findp(int s)
18 {
19     while(s!=parent[s])
20         s=parent[s];
21     return s;
22 }
23 double merge(scn1 s)
24 {
25     int x=findp(s.a);
26     int y=findp(s.b);
27     if(x!=y)
28     {
29         parent[y]=x;
30         return s.c;
31     }
32     return 0;
33 }
34 int main()
35 {
36     int n;
37     while(~scanf("%d",&n))
38     {
39         int i,j;
40         for(i=1;i<=n;i++)
41             scanf("%lf%lf",&sx[i],&sy[i]);
42         int k=1;
43         for(i=1;i<=n;i++)
44             for(j=i+1;j<=n;j++)
45             {
46                 scn[k].a=i;
47                 scn[k].b=j;
48                 scn[k].c=distance(sx[i],sy[i],sx[j],sy[j]);
49                 k++;
50             }
51         sort(scn+1,scn+k+2,comp);
52         for(i=1;i<=n;i++)
53             parent[i]=i;
54         double sum=0;
55         for(i=1;i<=k;i++)
56             sum+=merge(scn[i]);
57         printf("%.2lf\n",sum);
58     }
59     return 0;
60 }

 

 
posted @ 2012-08-17 00:10  crazy_apple  阅读(233)  评论(0编辑  收藏  举报