关闭

最小生成树

标签: 算法
280人阅读 评论(0) 收藏 举报
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. }  
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89338次
    • 积分:2911
    • 等级:
    • 排名:第12079名
    • 原创:209篇
    • 转载:19篇
    • 译文:0篇
    • 评论:10条
    最新评论