http://acm.hdu.edu.cn/showproblem.php?pid=1162
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4199 Accepted Submission(s): 2061
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?
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.
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
普利姆算法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #define INIT 0x7ffffff 6 double Graph[110][110]; 7 int vis[110]; 8 double dis[110]; 9 int n; 10 double sum; 11 int count; 12 void prime() 13 { 14 memset(vis,0,sizeof(vis)); 15 memset(dis,INIT,sizeof(dis)); 16 int i,j; 17 double min; 18 sum=0; 19 count=1; 20 int now; 21 for(i=1;i<=n;i++) 22 dis[i]=Graph[1][i]; 23 for(j=2;j<=n;j++) 24 { 25 min=INIT; 26 for(i=2;i<=n;i++) 27 { 28 if(min>dis[i]&&vis[i]==0) 29 { 30 min=dis[i]; 31 now=i; 32 } 33 } 34 if(min!=INIT) 35 { 36 count++; 37 sum+=min; 38 vis[now]=1; 39 for(i=1;i<=n;i++) 40 { 41 if(vis[i]==0&&Graph[now][i]!=0&&dis[i]>Graph[now][i]) 42 dis[i]=Graph[now][i]; 43 } 44 } 45 else return; 46 } 47 } 48 double length(double x1,double y1,double x2,double y2) 49 { 50 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 51 } 52 int main() 53 { 54 int i,j,k; 55 while(~scanf("%d",&n)) 56 { 57 for(i=1;i<=n;i++) 58 { 59 for(j=1;j<=n;j++) 60 { 61 if(i==j) Graph[i][j]=0; 62 else 63 { 64 Graph[i][j]=Graph[j][i]=INIT; 65 } 66 } 67 } 68 double xx[110],yy[110]; 69 for(i=1;i<=n;i++) 70 { 71 scanf("%lf%lf",&xx[i],&yy[i]); 72 } 73 for(i=1;i<=n;i++) 74 { 75 for(j=i+1;j<=n;j++) 76 { 77 Graph[i][j]=Graph[j][i]=length(xx[i],yy[i],xx[j],yy[j]); 78 } 79 } 80 prime(); 81 printf("%.2lf\n",sum); 82 } 83 }
克鲁斯卡尔
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 int root[110]; 6 double xx[110],yy[110]; 7 struct Node 8 { 9 int start; 10 int end; 11 double len; 12 }node[5010]; 13 int cmp(const void*x,const void* y) 14 { 15 return (*(Node*)x).len>(*(Node*)y).len?1:-1; 16 } 17 int find(int x) 18 { 19 int r=x; 20 while(r!=root[r]) 21 r=root[r]; 22 int i=x,j; 23 while(i!=r) 24 { 25 j=root[i]; 26 root[i]=r; 27 i=j; 28 } 29 return r; 30 } 31 void merge(int x,int y) 32 { 33 int fx,fy; 34 fx=find(x); 35 fy=find(y); 36 if(fx!=fy) root[fx]=fy; 37 } 38 double length(double x1,double y1,double x2,double y2) 39 { 40 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 41 } 42 int main() 43 { 44 int n; 45 int i,j,k; 46 while(~scanf("%d",&n)) 47 { 48 for(i=1;i<=n;i++) 49 root[i]=i; 50 for(i=1;i<=n;i++) 51 { 52 scanf("%lf%lf",&xx[i],&yy[i]); 53 } 54 k=0; 55 for(i=1;i<=n;i++) 56 { 57 for(j=i+1;j<=n;j++) 58 { 59 node[k].start=i; 60 node[k].end=j; 61 node[k].len=length(xx[i],yy[i],xx[j],yy[j]); 62 k++; 63 } 64 } 65 qsort(node,k,sizeof(node[0]),cmp); 66 double sum=0; 67 for(i=0;i<k;i++) 68 { 69 if(find(node[i].start)!=find(node[i].end)) 70 { 71 merge(node[i].start,node[i].end); 72 sum+=node[i].len; 73 } 74 } 75 printf("%.2lf\n",sum); 76 } 77 }