题目连接: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): 10085    Accepted Submission(s):
5094

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算法计算。
 
AC代码:
  1. 19975116 2017-03-03 08:53:42 Accepted 1162 0MS 1524K 1304 B G++
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct point
  10. {
  11. int x,y;
  12. double l;
  13. }p[];
  14. int parent[],n;
  15. double x[],y[];
  16. bool cmp(point a, point b)
  17. {
  18. return a.l < b.l;
  19. }
  20. int find(int x)
  21. {
  22. int s,tmp;
  23. for (s = x; parent[s] >= ; s = parent[s]);
  24. while (s != x)
  25. {
  26. tmp = parent[x];
  27. parent[x] = s;
  28. x = tmp;
  29. }
  30. return s;
  31. }
  32. void Union(int A, int B)
  33. {
  34. int a = find(A), b = find(B);
  35. int tmp = parent[a]+parent[b];
  36. if (parent[a] < parent[b])
  37. {
  38. parent[b] = a;
  39. parent[a] = tmp;
  40. }
  41. else
  42. {
  43. parent[a] = b;
  44. parent[b] = tmp;
  45. }
  46. }
  47. void kruskal(int k)
  48. {
  49. double sum = ;
  50. int u,v,i,j = ;
  51. memset(parent,-,sizeof(parent));
  52. for (i = ; i <= k; i ++)
  53. {
  54. u = p[i].x; v = p[i].y;
  55. if (find(u) != find(v))
  56. {
  57. sum += p[i].l;
  58. Union(u,v);
  59. j ++;
  60. }
  61. if (j == n-) break;
  62. }
  63. printf("%.2lf\n",sum);
  64. }
  65. int main ()
  66. {
  67. int i,j,k;
  68. while (scanf("%d",&n)!=EOF)
  69. {
  70. k = -;
  71. for (i = ; i <= n; i ++)
  72. scanf("%lf%lf",x+i,y+i);
  73. for (i = ; i < n; i ++)
  74. for (j = +i; j <= n; j ++)
  75. {
  76. p[++ k].x = i;
  77. p[k].y = j;
  78. p[k].l = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
  79. }
  80. sort(p,p+k+,cmp);
  81. kruskal(k);
  82. }
  83. return ;
  84. }

算法理解:http://www.cnblogs.com/yoke/p/6506492.html

hdu 1162 Eddy's picture (Kruskal 算法)的更多相关文章

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

    题目链接: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 (最小生成树)(java版)

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

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

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

  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(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...

随机推荐

  1. HNOI2018 退役记

    HNOI2018 退役记 \(day0\): 除了切水题以外没有什么很重要的事. \(day1\): 进考场发现前面是\(yyb\)?\(orzyyb\) 试题解压密码终于没有奇怪的字符了,一遍打对. ...

  2. crontab例行性共作

    一.单一工作调度 at [-mldv] TIME at -c 工作号码 -m:当at工作结束后,即是没有输出信息,以email通知用户该工作已完成 -l:at -l相当于atq,列出目前系统上所有的a ...

  3. ABP与多租户

    ABP简介 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用程 ...

  4. 【python】-matplotlib.pylab常规用法

    目的: 了解matplotlib.pylab常规用法 示例 import matplotlib.pylab as pl x = range(10) y = [i * i for i in x] pl. ...

  5. 高并发和多线程——Java内存模型

    个人理解: 1.并发编程分为三个核心:分工.同步.互斥 2.CPU.内存.I/O存在速度差异问题. 3.线程切换导致原子性问题.编译优化导致有序性问题.缓存导致可见性问题. 4.Java内存模型规范了 ...

  6. openstf安装手记

    一款基于nodejs+jade模板开发的手机群控框架,你可以在此基础上拿来测试设备,或者群操作等等. 主要核心技术在于如何在不root的情况下实时流畅录屏,以及远程操作手机. 官方git https: ...

  7. selenium自动化测试用例需要关注的几点(二)

    注:原文地址 UI映射 一个UI映射是一种机制,它存储所有的定位器的测试套件在一个地方,方便修改UI元素的路径标识符或改变在AUT.测试脚本,然后使用UI地图定位以被测试的元件.基本上,UI地图是一个 ...

  8. HTML5离线资源缓存简介

    cache manifest 示例 要使用离线资源缓存,开发者首先要提供一个 cache manifest 文件 它列出了所有需要在离线状态下使用的资源,浏览器会把这些资源缓存到本地 下面就是一个 c ...

  9. OpenGL初识

    OpenGL 概念 OpenGL提供的是一系列接口, 它是指一个规范, OpenGL规范严格规定了每个函数该如何执行, 以及它们的输出值, 具体的实现是由各个显示设备厂商, 它作为本地系统库直接运行在 ...

  10. Java入门系列-20-异常

    为什么要进行异常处理 下面这段代码能否正常执行 public class DemoCalc { public static void main(String[] args) { int a=0; in ...