最小生成树

原创 2015年07月06日 18:40:04
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2835    Accepted Submission(s): 1377


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
 

ac代码:

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <cstdio>  
  4. #include <cmath>  
  5. using namespace std;  
  6. const int N=10005;  
  7. double leftt[N],rightt[N];  
  8. //double value[N];  
  9. int father[N],r[N];  
  10. struct point{  
  11.   int x,y;  
  12.   double value;  
  13. }aa[10005];  
  14. int cmp(const int i,const int j){  
  15.     return aa[i].value<aa[j].value;  
  16. }  
  17. int find(int x){  
  18.   if(father[x]!=x)  
  19.       father[x]=find(father[x]);  
  20.   return father[x];  
  21. }  
  22. int main(){  
  23.   int n;  
  24.   while(scanf("%d",&n)!=EOF){  
  25.       for(int i=0;i<N;++i){  
  26.         father[i]=i;  
  27.         r[i]=i;  
  28.         aa[i].value=0.0;  
  29.       }  
  30.        /*for(int i=0;i<10;++i) 
  31.       {printf("%d  ",r[i]);}*/  
  32.       for(int i=0;i<n;++i){  
  33.         scanf("%lf%lf",&leftt[i],&rightt[i]);  
  34.       }  
  35.       int k=0;  
  36.       for(int i=0;i<n;++i){  
  37.           for(int j=0;j<n;++j){  
  38.               if(i!=j)  
  39.               {aa[k].value=sqrt((leftt[i]-leftt[j])*(leftt[i]-leftt[j])+(rightt[i]-rightt[j])*(rightt[i]-rightt[j]));  
  40.                 aa[k].x=i;  
  41.                 aa[k].y=j;  
  42.                 k++;  
  43.               }  
  44.           }  
  45.       }  
  46.       //printf("k===%d\n",k);  
  47.       /*for(int i=0;i<k;++i) 
  48.       {printf("%d  ",r[i]);}*/  
  49.       sort(r,r+k,cmp);  
  50.       /*for(int i=0;i<k;++i) 
  51.       {printf("%d  ",r[i]);} 
  52.       printf("\n");*/  
  53.       double sum=0;  
  54.       for(int i=0;i<k;++i){  
  55.         int e=r[i];  
  56.         int x=find(aa[e].x);  
  57.          int y=find(aa[e].y);  
  58.         if(x!=y){  
  59.             sum+=aa[e].value;  
  60.           /*printf("x==%d\n  y==%d\n",x,y); 
  61.           printf("sum===%.2lf\n",sum);*/  
  62.           father[x]=y;  
  63.         }  
  64.       }  
  65.       printf("%.2lf\n",sum);  
  66.   }  
  67.   return 0;  
  68. }  
版权声明:本文为博主http://www.feixueteam.net原创文章,未经博主允许不得转载。

相关文章推荐

最小生成树及其拓展ppt

  • 2017-07-09 11:20
  • 127KB
  • 下载

最小生成树之prim

福州大学第十四届程序设计竞赛_重现赛 J - 英语考试 (最小生成树 )

在过三个礼拜,YellowStar有一场专业英语考试,因此它必须着手开始复习。 这天,YellowStar准备了n个需要背的单词,每个单词的长度均为m。 YellowSatr准备采用联想记忆法来背...

matlab 最小生成树程序

  • 2015-09-13 17:19
  • 636B
  • 下载

最小生成树

最小生成树Prim算法理解

MST(Minimum Spanning Tree,最小生成树)问题有两种通用的解法,Prim算法就是其中之一,它是从点的方面考虑构建一颗MST,大致思想是:设图G顶点集合为U,首先任意选择图G中的一...

数据结构 PRIM最小生成树

  • 2014-05-29 20:52
  • 1.06MB
  • 下载

最小生成树项目

  • 2014-07-01 20:19
  • 112KB
  • 下载

算法导论 | 第23章 最小生成树

最小生成树(Minimum Spanning Tree),全称“最小权值生成树” 有两种具体的实现算法 1.Kruskal算法 2.Prim算法 两者都用到了贪心算法。

最小生成树

  • 2013-05-06 08:49
  • 950KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)