HDU 3007 Buried memory 最小圆覆盖

题目大意:没看。反正就是求最小圆覆盖。

思路:一个神奇的算法——随机增量法。可以证明,这个算法可以在O(n)的时间复杂度内求出最小圆覆盖。虽然好像能卡掉的样子,但是加上一句random_shuffle就卡不掉了。

具体的过程是这样的:

在全局记录一个圆,表示目前的最小圆覆盖。从头开始扫描。遇到第一个不在当前最小圆覆盖内的点的时候:

将这个点与当前最小圆覆盖的圆心为直径做一个圆,作为当前的最小圆覆盖。从头开始扫描。遇到第一个不在当前最小圆覆盖的点的时候:

将刚才的两个点和当前点做三角形,将这个三角形的外接圆作为当前的最小圆覆盖。

具体还是看代码吧,关于证明见:http://blog.csdn.net/lthyxy/article/details/6661250

CODE:

#define _CRT_SECURE_NO_WARNINGS

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 510
using namespace std;

struct Point{
	double x,y;

	Point(double _,double __):x(_),y(__) {}
	Point() {}
	Point operator +(const Point &a)const {
		return Point(x + a.x,y + a.y);
	}
	Point operator -(const Point &a)const {
		return Point(x - a.x,y - a.y);
	}
	Point operator *(double a)const {
		return Point(x * a,y * a);
	}
	void Read() {
		scanf("%lf%lf",&x,&y);
	}
}point[MAX];

inline double Calc(const Point &p1,const Point &p2)
{
	return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

inline Point Mid(const Point &p1,const Point &p2)
{
	return Point((p1.x + p2.x) / 2,(p1.y + p2.y) / 2);
}

inline double Cross(const Point &p1,const Point &p2)
{
	return p1.x * p2.y - p1.y * p2.x;
}

inline Point Change(const Point &v)
{
	return Point(-v.y,v.x);
}

struct Circle{
	Point o;
	double r;

	Circle(const Point &_,double __):o(_),r(__) {}
	Circle() {}
	bool InCircle(const Point &p) {
		return Calc(p,o) <= r;
	}
}now;

struct Line{
	Point p,v;

	Line(const Point &_,const Point &__):p(_),v(__) {}
	Line() {}
};

inline Point GetIntersection(const Line &l1,const Line &l2)
{
	Point u = l1.p - l2.p;
	double t = Cross(l2.v,u) / Cross(l1.v,l2.v);
	return l1.p + l1.v * t;
}

int points;

int main()
{
	while(scanf("%d",&points),points) {
		for(int i = 1; i <= points; ++i)
			point[i].Read();
		random_shuffle(point + 1,point + points + 1);
		now = Circle(point[1],.0);
		for(int i = 2; i <= points; ++i)
			if(!now.InCircle(point[i])) {
				now = Circle(point[i],.0);
				for(int j = 1; j < i; ++j)
					if(!now.InCircle(point[j])) {
						now = Circle(Mid(point[i],point[j]),Calc(point[i],point[j]) / 2);
						for(int k = 1; k < j; ++k)
							if(!now.InCircle(point[k])) {
								Line l1(Mid(point[i],point[j]),Change(point[j] - point[i]));
								Line l2(Mid(point[j],point[k]),Change(point[k] - point[j]));
								Point intersection = GetIntersection(l1,l2);
								now = Circle(intersection,Calc(point[i],intersection));
							}
					}
			}
		printf("%.2lf %.2lf %.2lf\n",now.o.x,now.o.y,now.r);
	}
	return 0;
}

时间: 2024-08-03 15:17:22

HDU 3007 Buried memory 最小圆覆盖的相关文章

hdu 3007 Buried memory 最远点对

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather

HDOJ 3007 Buried memory 增量法最小圆覆盖

增量法最小圆覆盖,简单模版 Buried memory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2541    Accepted Submission(s): 1365 Problem Description Each person had do something foolish along with his or her g

【HDOJ】3007 Buried memory

1. 题目描述有n个点,求能覆盖这n个点的半径最小的圆的圆心及半径. 2. 基本思路算法模板http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066定义Di表示相对于P[1]和P[i]组成的最小覆盖圆,如果P[2..i-1]都在这个圆内,那么当前的圆心和半径即为最优解.如果P[j]不在这个圆内,那么P[j]一定在新的最小覆盖圆的边界上即P[1].P[j].P[i]组成的圆.因为三点可以确定一个圆,因此只需要不断的找到不满足的P[j],进而更新最优解即可.其

最小圆覆盖 hdu 3007

今天学习了一下最小圆覆盖, 看了一下午都没看懂, 晚上慢慢的摸索这代码,接合着别人的讲解, 画着图跟着代码一步一步的走着,竟然有些理解了. 最小圆覆盖: 给定n个点, 求出半径最小的圆可以把这些点全部包围, 可以在圆的边界上 下面是我的个人理解. 如果不对, 还请路过大牛指出 先找一个点, 让圆心等于这个点的坐标, 半径等于0, 显然这个对的, 接着找下一个点, 如果只有两个点的话, 那么最小的圆一定是以他们为直径做圆, 接着找第三个点, 如果第三个点在园内或者在边界上, 那么不用更新当前的最小

HDOJ 2215 Maple trees 最小圆覆盖

增量法最小圆覆盖.... Maple trees Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1646    Accepted Submission(s): 510 Problem Description There are a lot of trees in HDU. Kiki want to surround all the t

HDU 3007

基本小圆覆盖模板题 #include <iostream> #include <algorithm> #include <cmath> using namespace std; int X,Y; int n; const double eps=0.00000001; struct point{ double x,y; }p[550]; struct Circle{ point cent; double r; }cir; double dist(point x, poin

【BZOJ-1336&amp;1337】Alie最小圆覆盖 最小圆覆盖(随机增量法)

1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1573  Solved: 697[Submit][Status][Discuss] Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0) Outpu

Bzoj 1336&amp;1337 Alien最小圆覆盖

1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special Judge Submit: 1473  Solved: 648 [Submit][Status][Discuss] Description Input 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0) Output Sample Input 6 8.0

BZOJ1337: 最小圆覆盖

题目:求n个点的最小圆覆盖. 题解:最小圆覆盖,上模板.复杂度证明可以戳:这里 代码: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #inclu