题意:给出n个点,求最小包围圆。

解法:这两天一直在学这个神奇的随机增量算法……看了这个http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066之后自己写了好久一直写不对……后来在计算几何的模板上找到了…………orz膜拜一下

代码:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string>
  5. #include<string.h>
  6. #include<math.h>
  7. #include<limits.h>
  8. #include<time.h>
  9. #include<stdlib.h>
  10. #include<map>
  11. #include<queue>
  12. #include<set>
  13. #include<stack>
  14. #include<vector>
  15. #define LL long long
  16. using namespace std;
  17. const double eps = 1e-8;
  18. int n;
  19. struct point
  20. {
  21. double x, y;
  22. }p[505];
  23. bool dy(double x, double y)//x > y
  24. {
  25. return x > y + eps;
  26. }
  27. double disp2p(point a, point b)//两点距离
  28. {
  29. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  30. }
  31. point l2l_inst_p(point u1, point u2, point v1, point v2)//两直线交点
  32. {
  33. point ans = u1;
  34. double t = ((u1.x - v1.x) * (v1.y - v2.y) - (u1.y - v1.y) * (v1.x - v2.x)) /
  35. ((u1.x - u2.x) * (v1.y - v2.y) - (u1.y - u2.y) * (v1.x - v2.x));
  36. ans.x += (u2.x - u1.x) * t;
  37. ans.y += (u2.y - u1.y) * t;
  38. return ans;
  39. }
  40. point circumcenter(point a, point b, point c)//三角形外接圆
  41. {
  42. point ua, ub, va, vb;
  43. ua.x = (a.x + b.x) / 2;
  44. ua.y = (a.y + b.y) / 2;
  45. ub.x = ua.x - a.y + b.y;
  46. ub.y = ua.y + a.x - b.x;
  47. va.x = (a.x + c.x) / 2;
  48. va.y = (a.y + c.y) / 2;
  49. vb.x = va.x - a.y + c.y;
  50. vb.y = va.y + a.x - c.x;
  51. return l2l_inst_p(ua, ub, va, vb);
  52. }
  53. void min_cover_circle(point &c, double &r)//最小包围圆
  54. {
  55. random_shuffle(p, p + n);//貌似是随机排序用的……
  56. c = p[0];
  57. r = 0;
  58. for(int i = 1; i < n; i++)
  59. if(dy(disp2p(p[i], c), r))
  60. {
  61. c = p[i];
  62. r = 0;
  63. for(int k = 0; k < i; k++)
  64. if(dy(disp2p(p[k], c), r))
  65. {
  66. c.x = (p[i].x + p[k].x) / 2;
  67. c.y = (p[i].y + p[k].y) / 2;
  68. r = disp2p(p[k], c);
  69. for(int j = 0; j < k; j++)
  70. if(dy(disp2p(p[j], c), r))
  71. {
  72. c = circumcenter(p[i], p[k], p[j]);
  73. r = disp2p(p[i], c);
  74. }
  75. }
  76. }
  77. }
  78. int main()
  79. {
  80. while(scanf("%d", &n) && n)
  81. {
  82. for(int i = 0; i < n; i++)
  83. scanf("%lf%lf", &p[i].x, &p[i].y);
  84. point c;
  85. double r;
  86. min_cover_circle(c, r);
  87. printf("%.2lf %.2lf %.2lf\n", c.x, c.y, r);
  88. }
  89. return 0;
  90. }

  

HDU 3007 Buried memory & ZOJ 1450 Minimal Circle的更多相关文章

  1. hdu 3007 Buried memory 最远点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...

  2. zoj 1450 Minimal Circle 最小覆盖圆

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=450 You are to write a program to fi ...

  3. HDU 3007 Buried memory(计算几何の最小圆覆盖,模版题)

    Problem Description Each person had do something foolish along with his or her growth.But,when he or ...

  4. ZOJ 1450 Minimal Circle 最小圆覆盖

    套了个模板直接上,貌似没有随机化序列 QAQ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #in ...

  5. HDU - 3007 Buried memory

    传送门 最小圆覆盖模板. //Achen #include<algorithm> #include<iostream> #include<cstring> #inc ...

  6. 【HDOJ】3007 Buried memory

    1. 题目描述有n个点,求能覆盖这n个点的半径最小的圆的圆心及半径. 2. 基本思路算法模板http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066定义Di表示 ...

  7. HDU 3007 模拟退火算法

    Buried memory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. ZOJ1450 Minimal Circle

    You are to write a program to find a circle which covers a set of points and has the minimal area. T ...

  9. HDU 3007

    基本小圆覆盖模板题 #include <iostream> #include <algorithm> #include <cmath> using namespac ...

随机推荐

  1. python 练习题

    python是一个功能很强大的语言,他可以解决各种在数学问题,下面我分享一些练习题供大家参考: 有关正态分布的问题: # -*- coding: cp936 -*- import math a=0 u ...

  2. sql之表的表达式

    1.派生表 实质:就是特殊的子查询(将查询结果放在from后面) 含有一张Order表: 看下面的sql语句: select orderid,orderdate,custid from ( selec ...

  3. UIApplication深入研究

    我们偶尔会调用这个类的api来实现一些功能,但是这个类是iOS编程中很重要的一个概念,所以总结以下这个类的信息,不对的地方请留言. UIApplication的核心作用是提供了iOS程序运行期间的控制 ...

  4. 1063: [Noi2008]道路设计 - BZOJ

    Description Z 国坐落于遥远而又神奇的东方半岛上,在小Z 的统治时代公路成为这里主要的交通手段.Z 国共有n 座城市,一些城市之间由双向的公路所连接.非常神奇的是Z 国的每个城市所处的经度 ...

  5. 2001: [Hnoi2010]City 城市建设 - BZOJ

    DescriptionPS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁.Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费.Louis希望建造最少的 ...

  6. jQuery中的join方法

    和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...

  7. notepad++ 开启/关闭 记住最后打开的文件

    开启记住最后打开的文件 1) 6.3以前版本如下设置: 设置-->首选项-->其他 把左下角的 "记住最后打开文件" 勾选. 2) 6.3以后版本如下设置: 设置--& ...

  8. VIM的高级使用

    VIM的高级使用  转:http://www.cnblogs.com/itech/archive/2012/02/22/2363111.html 1)一些常用的Vim配置,在~/.vimrc中 syn ...

  9. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  10. rand5()产生rand7()

    http://www.cnblogs.com/dwdxdy/archive/2012/07/28/2613135.html 利用rand5()产生rand7().rand5()产生1到5的整数,ran ...