hdu 1162 Eddy's picture (最小生成树)

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6339    Accepted Submission(s): 3179


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
 

 

Author
eddy
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1301 1213 1116 1269 1198 
 

 

最小生成树入门题:

 1 //0MS     384K    1215B     G++
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 struct point{
 6     double x,y;
 7 }p[105];
 8 struct node{
 9     int u,v;
10     double d;
11 }t[10005];
12 int set[105],n;
13 int cmp(const void*a,const void*b)
14 {
15     return (*(node*)a).d>(*(node*)b).d?1:-1;
16 }
17 int find(int x)
18 {
19     if(set[x]!=x) set[x]=find(set[x]);
20     return set[x];
21 }
22 int merge(int a,int b)
23 {
24     int x=find(a);
25     int y=find(b);
26     if(x!=y){
27         set[x]=y;
28         return 1;
29     }
30     return 0;
31 }
32 double kruskal()
33 {
34     double ans=0;
35     for(int i=0;i<n*n;i++)
36         if(merge(t[i].u,t[i].v))
37             ans+=t[i].d;
38     return ans;
39 }
40 double dis(point a,point b)
41 {
42     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
43 }
44 int main(void)
45 {
46     while(scanf("%d",&n)!=EOF)
47     {
48         for(int i=0;i<=n;i++) set[i]=i;
49         for(int i=0;i<n;i++)
50             scanf("%lf%lf",&p[i].x,&p[i].y);
51         int k=0;
52         for(int i=0;i<n;i++)
53            for(int j=0;j<n;j++){
54                t[k].u=i;
55                t[k].v=j;
56                t[k].d=dis(p[i],p[j]);
57                k++;
58            }
59         qsort(t,n*n,sizeof(t[0]),cmp);
60         printf("%.2lf\n",kruskal());
61     }
62     return 0;
63 }

 

posted @ 2014-05-05 21:02  heaventouch  阅读(226)  评论(0编辑  收藏  举报