题目链接

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

  1. 3
  2. 1.0 1.0
  3. 2.0 2.0
  4. 2.0 4.0

Sample Output

  1. 3.41

分析:

题上要求的是用最短的线将坐标系上的n个点链接起来,这就是简单的最小生成树的问题,将任意两个点之间的距离计算出来,看作他们之间的路径长度,这样的话就能很好的理解为什么是最小生成树的问题了。

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #define INF 1 << 30
  5. double a[101] , b[101] , map[101][101] ;
  6. double dis[101] ;
  7. int used[101] ;
  8. void prim(int n)
  9. {
  10. int c = 0 ;
  11. int i = 0 , j = 0 ;
  12. double sum = 0 ;
  13. dis[1] = 0 ;
  14. for(i = 1 ; i <= n ; i++)
  15. {
  16. double min = INF ;
  17. c = 0 ;
  18. for(j = 1 ; j <= n ; j++)
  19. {
  20. if(!used[j] && dis[j] < min)
  21. {
  22. min = dis[j] ;
  23. c =j ;
  24. }
  25. }
  26. used[c] = 1 ;
  27. for(j = 1 ; j <= n ; j++ )
  28. {
  29. if(!used[j] && dis[j] > map[c][j])
  30. dis[j] = map[c][j] ;
  31. }
  32. }
  33. for(i = 1 ; i <= n ; i++)
  34. sum += dis[i] ;
  35. printf("%.2lf\n",sum);
  36. }
  37. int main()
  38. {
  39. int n = 0 ;
  40. while(~scanf("%d",&n))
  41. {
  42. memset(a , 0 , sizeof( a ) ) ;
  43. memset(b , 0 , sizeof( b ) ) ;
  44. int i = 0 , j = 0 ;
  45. for(i = 1 ; i <= n ; i++)
  46. {
  47. for(j = 1 ; j <= n ; j++)
  48. map[i][j] = INF ;
  49. dis[i] = INF ;
  50. used[i] = 0 ;
  51. }
  52. for(i = 1 ; i <= n ; i++)
  53. {
  54. scanf("%lf%lf" , &a[i] , &b[i] );///切记不能向一般的图论的问题,在输入的同时保存路径
  55. }
  56. double m = 0 , x = 0;
  57. for(i = 1 ; i <= n ; i++ )///应该在输入结束之后,将任意两点之间的路径计算出来
  58. {
  59. for(j = 1 ; j <= n ; j++)
  60. {
  61. x = (a[j]-a[i])*(a[j]-a[i])+(b[j]-b[i])*(b[j]-b[i]) ;
  62. m = sqrt( x ) ;
  63. map[i][j] = map[j][i] = m ;
  64. }
  65. }
  66. prim( n );
  67. }
  68. return 0 ;
  69. }

HDU 1162 Eddy's picture (最小生成树 普里姆 )的更多相关文章

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

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

  4. 图->连通性->最小生成树(普里姆算法)

    文字描述 用连通网来表示n个城市及n个城市间可能设置的通信线路,其中网的顶点表示城市,边表示两城市之间的线路,赋于边的权值表示相应的代价.对于n个定点的连通网可以建立许多不同的生成树,每一棵生成树都可 ...

  5. 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 ...

  6. 图解最小生成树 - 普里姆(Prim)算法

    我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...

  7. 最小生成树---普里姆算法(Prim算法)和克鲁斯卡尔算法(Kruskal算法)

    普里姆算法(Prim算法) #include<bits/stdc++.h> using namespace std; #define MAXVEX 100 #define INF 6553 ...

  8. HDU 1162 Eddy's picture

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

  9. hdu 1162 Eddy's picture (prim)

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

随机推荐

  1. html基础问题总结

    1.reflow 在CSS规范中有一个渲染对象的概念,通常用一个盒子(box, rectangle)来表示.mozilla通过一个叫frame的对象对盒子进行操作.frame主要的动作有三个: 构造f ...

  2. (干货分享)mac python+appium环境搭建

    因为mac本自带python2.x(不建议卸载,因为本本本身有很多依赖与此),所以装python3的过程极其坎坷,勉强装好后也总是各种报错.这次装appium环境,直接把原来的python3卸了,用h ...

  3. Google无法离线安装扩展程序

    Google无法离线安装扩展程序 Chrome插件伴侣 按照里面的使用说明使用 网盘地址: 链接: https://pan.baidu.com/s/1eXoLXyPNl2pfoPnArHq2Lg 提取 ...

  4. (原创)BFS广度优先算法,看完这篇就够了

    BFS算法 上一篇文章讲解了DFS深度优先遍历的算法,我们说 DFS 顾名思义DEEPTH FIRET,以深度为第一标准来查找,以不撞南墙不回头的态度来发掘每一个点,这个算法思想get到了其实蛮简单. ...

  5. Ubuntu18.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook深度学习环境配置

    目录 一.Ubuntu18.04 LTS系统的安装 1. 安装文件下载 2. 制作U盘安装镜像文件 3. 开始安装 二.设置软件源的国内镜像 1. 设置方法 2.关于ubuntu镜像的小知识 三.Nv ...

  6. OpenPAI:大规模人工智能集群管理平台介绍及任务提交指南

    产品渊源: 随着人工智能技术的快速发展,各种深度学习框架层出不穷,为了提高效率,更好地让人工智能快速落地,很多企业都很关注深度学习训练的平台化问题.例如,如何提升GPU等硬件资源的利用率?如何节省硬件 ...

  7. C++STL——list

    一.相关定义 list 链表,分配的内存不连续 可以高效地进行插入/删除元素 不可随机访问,访问速度慢 特征 只能通过迭代器来访问list中的元素 在头和尾都可以插入元素 二.list [前提条件] ...

  8. Android之ViewPager 第二课

    在这里只粘贴部分代码 在第一课中,只有View滑动完毕,才触发动画效果,令滑块移动,在第二课中,将实现滑块与View同步运行. SecondActivity.java package com.andr ...

  9. 山科SDUST OJ Problem J :连分数

    Problem J: 连分数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2723  Solved: 801[Submit][Status][Web B ...

  10. 团队作业4——第一次项目冲刺(Alpha版本)-第二篇

    项目冲刺——第二阶段 度过了敏捷冲刺,各个成员积极汇报各自的工作.好了,着手下一步规划! Mission 团队成员 任务 郭达  实现PHP后台的答题判分查看正确率 刘德培  编写博客 石浩洋  实现 ...