并查集+最小生成树

Eddy's picture

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

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
 
kruskal算法
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<math.h>
  4. using namespace std;
  5. int set[110];
  6. struct record//注意此题要用实型
  7. {
  8. double a;
  9. double b;
  10. double ju;//两点之间距离
  11. }s[10000];
  12. int find(int fa)
  13. {
  14. int ch=fa;
  15. int t;
  16. while(fa!=set[fa])
  17. fa=set[fa];
  18. while(ch!=fa)
  19. {
  20. t=set[fa];
  21. set[ch]=fa;
  22. ch=t;
  23. }
  24. return fa;
  25. }
  26. void mix(int x,int y)
  27. {
  28. int fx,fy;
  29. fx=find(x);
  30. fy=find(y);
  31. if(fx!=fy)
  32. set[fx]=fy;
  33. }
  34. double dis(double x1,double y1,double x2,double y2)
  35. {
  36. return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); //计算两点之间距离
  37. }
  38. bool cmp(record c,record d)
  39. {
  40. return c.ju<d.ju;
  41. }
  42. int main()
  43. {
  44. int m,j,i,point;
  45. double sum;
  46. double a[110];//这两个数组用来储存点的坐标,a数组储存横标
  47. double b[110];//b数组储存纵标
  48. while(scanf("%d",&point)!=EOF)
  49. {
  50. for(i=0;i<=point;i++)
  51. set[i]=i;
  52. for(i=1;i<=point;i++)
  53. {
  54. scanf("%lf%lf",&a[i],&b[i]);
  55. }
  56. m=0;
  57. for(i=1;i<=point-1;i++) //此循环求任意两点之间距离
  58. { //并记录下此两点位置
  59. for(j=i+1;j<=point;j++)
  60. {
  61. s[m].ju=dis(a[i],b[i],a[j],b[j]);
  62. s[m].a=i;
  63. s[m].b=j;
  64. m++;
  65. }
  66. }
  67. sort(s,s+m,cmp);
  68. sum=0;
  69. for(i=0;i<m;i++)
  70. {
  71. if(find(s[i].a)!=find(s[i].b))
  72. {
  73. mix(s[i].a,s[i].b);
  74. sum+=s[i].ju;
  75. }
  76. }
  77. printf("%.2lf\n",sum);
  78. }
  79. return 0;
  80. }

  

 

hdoj 1162 Eddy's picture的更多相关文章

  1. hdu 1162 Eddy's picture (Kruskal 算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  2. hdu 1162 Eddy's picture(最小生成树算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  3. HDU 1162 Eddy's picture (最小生成树)(java版)

    Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...

  4. HDU 1162 Eddy's picture

    坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

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

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. hdu 1162 Eddy's picture (prim)

    Eddy's pictureTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. HDU 1162 Eddy's picture (最小生成树 prim)

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  8. HDU 1162 Eddy's picture (最小生成树 普里姆 )

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  9. 题解报告:hdu 1162 Eddy's picture

    Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...

随机推荐

  1. ABC: Always Be Coding——程序员面试必

    本文作者@guitardave24 ">David Byttow 是一名程序员,曾在 Google 和 Square 等公司工作过. 在正文之前,先让我们回答几个简单的问题:第一,你面 ...

  2. 李洪强iOS开发本人集成环信的经验总结_08_自动登录补充

    李洪强iOS开发本人集成环信的经验总结_08_自动登录补充 来到Appdelegate里面 01 - 遵守自动登录的代理协议 02 - 设置自动登录的代理 03 - 判断与实现  04 - 代理方法的 ...

  3. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  4. centos安装异常解决方法

    VMware系统安装Centos7后,第一次启动出现以下异常信息: Initial setup of CentOS Linux (core) ) [x] Creat user ) [!] Licens ...

  5. C#中种常用的计时器

    1.System.Timers.Timer和System.Windows.Forms.Timer,它的最低识别为1/18s. 2.timeGetTime,他的最低识别能达到5ms. 3.System. ...

  6. ogg实现oracle到sql server 2005的同步

    一.源端(oracle)配置1.创建同步测试表create table gg_user.t01(name varchar(20) primary key);create table gg_user.t ...

  7. 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求

    具体设置如下: 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Setti ...

  8. OWIN katana注册中间件的几种写法

    首先特别说明下在startup中注册完中间件的两个注意事项,看到有人写的东西有误导人的作用.关于startup启动发现类的内容,参照这里 http://www.asp.net/aspnet/overv ...

  9. UVALive 3713 Astronauts (2-SAT,变形)

    题意: 有A,B,C三种任务,每个人必获得1个任务,大于等于平均年龄的可以选择A和C,小于平均年龄的可以选择B和C.这些人有一些是互相讨厌的,必须不能执行同任务,问能否安排他们工作?若行,输出任意一组 ...

  10. ↗☻【HTML5秘籍 #BOOK#】第1章 HTML5简介

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...