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

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

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
const double eps = 1e-8;
int n;
struct point
{
double x, y;
}p[505];
bool dy(double x, double y)//x > y
{
return x > y + eps;
}
double disp2p(point a, point b)//两点距离
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
point l2l_inst_p(point u1, point u2, point v1, point v2)//两直线交点
{
point ans = u1;
double t = ((u1.x - v1.x) * (v1.y - v2.y) - (u1.y - v1.y) * (v1.x - v2.x)) /
((u1.x - u2.x) * (v1.y - v2.y) - (u1.y - u2.y) * (v1.x - v2.x));
ans.x += (u2.x - u1.x) * t;
ans.y += (u2.y - u1.y) * t;
return ans;
}
point circumcenter(point a, point b, point c)//三角形外接圆
{
point ua, ub, va, vb;
ua.x = (a.x + b.x) / 2;
ua.y = (a.y + b.y) / 2;
ub.x = ua.x - a.y + b.y;
ub.y = ua.y + a.x - b.x;
va.x = (a.x + c.x) / 2;
va.y = (a.y + c.y) / 2;
vb.x = va.x - a.y + c.y;
vb.y = va.y + a.x - c.x;
return l2l_inst_p(ua, ub, va, vb);
}
void min_cover_circle(point &c, double &r)//最小包围圆
{
random_shuffle(p, p + n);//貌似是随机排序用的……
c = p[0];
r = 0;
for(int i = 1; i < n; i++)
if(dy(disp2p(p[i], c), r))
{
c = p[i];
r = 0;
for(int k = 0; k < i; k++)
if(dy(disp2p(p[k], c), r))
{
c.x = (p[i].x + p[k].x) / 2;
c.y = (p[i].y + p[k].y) / 2;
r = disp2p(p[k], c);
for(int j = 0; j < k; j++)
if(dy(disp2p(p[j], c), r))
{
c = circumcenter(p[i], p[k], p[j]);
r = disp2p(p[i], c);
}
}
}
}
int main()
{
while(scanf("%d", &n) && n)
{
for(int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
point c;
double r;
min_cover_circle(c, r);
printf("%.2lf %.2lf %.2lf\n", c.x, c.y, r);
}
return 0;
}

  

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. Java类加载器加载类顺序

    java ClassLoader的学习 java是一门解释执行的语言,由开发人员编写好的java源文件先编译成字节码文件.class形式,然后由java虚拟机(JVM)解释执 行,.class字节码文 ...

  2. JSON操作,转载

    http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html#_label0

  3. ASIHTTPRequest的使用(转)

    转载自:http://fushengfei.iteye.com/blog/1147112 博客分类: IOS   原文地址:http://wiki.magiche.net/pages/viewpage ...

  4. 严重: The web application [] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it.

    今日在重新部署项目时出现此问题,虽然对项目无影响,但问题就是问题.完整信息如下(使用idea工具): 十二月 05, 2015 11:44:27 上午 org.apache.catalina.star ...

  5. 关于ax+by=c的解x,y的min(|x|+|y|)值问题

    首先我们移动一下项,并强行让a>b. 然后我们可以画出这样一个图像 我们发现,在线段l与x轴交点处的下方,x,y的绝度值是递增的,所以我们不考虑那个最小点在下端. 之后我们发现在点的上端,因为斜 ...

  6. PAT-乙级-1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  7. 团体程序设计天梯赛-练习集L2-005. 集合相似度

    L2-005. 集合相似度 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定两个整数集合,它们的相似度定义为:Nc/Nt*1 ...

  8. 以守护进程方式启动firefly

    原地址:http://www.9miao.com/question-15-53966.html 最近看源码,查了半天,没找到已守护进程方式启动firefly的方法,自己改了改写了一个,废话不多说直接上 ...

  9. 线索二叉树Threaded binary tree

    摘要   按照某种遍历方式对二叉树进行遍历,可以把二叉树中所有结点排序为一个线性序列.在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结点.这 ...

  10. Eclipse 安装FindBugs插件

    FindBugs 是由马里兰大学提供的一款开源 Java静态代码分析工具.FindBugs通过检查类文件或 JAR文件,将字节码与一组缺陷模式进行对比从而发现代码缺陷,完成静态代码分析.FindBug ...