poj 3525 Most Distant Point from the Sea(半平面交+二分)

Most Distant Point from the Sea
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5136 Accepted: 2323 Special Judge

Description

The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.

In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.

Input

The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.

n  
x1 y1
  
xn yn

Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.

n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xiyi)–(xi+1yi+1) (1 ≤ i ≤ n − 1) and the line segment (xnyn)–(x1y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.

You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.

The last dataset is followed by a line containing a single zero.

Output

For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input

4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0

Sample Output

5000.000000
494.233641
34.542948
0.353553

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:求凸包中的点到凸包边界的最远距离,即凸多边形的最大内切圆。

题解:二分+半平面交。

每次二分一个长度,然后让凸多边形的每条边向内缩,以缩完后的边界做半平面交,如果不存在公共区域或者公共区域为一个点,则说明长度大了或正好,继续二分即可。

主要是内缩比较难搞,我是把相邻两点构成的向量存储下来,然后按照法向量的方向平移得到新的向量,再进行半平面交的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 103
#define eps 1e-7
#define inf 1e9
using namespace std;
struct vector {
	double x,y;
	vector (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],p[N],tmp[N],b[N];
struct data
{
	vector a,b;
}line[N];
int m,n;
double pi=acos(-1.0);
typedef vector point;
vector operator -(vector a,vector b){return vector (a.x-b.x,a.y-b.y);}
vector operator +(vector a,vector b){return vector (a.x+b.x,a.y+b.y);}
vector operator *(vector a,double t){return vector (a.x*t,a.y*t);}
vector operator /(vector a,double t){return vector (a.x/t,a.y/t);}
void init()
{
	m=0;
	p[m++]=point(inf,inf);
	p[m++]=point(-inf,inf);
	p[m++]=point(-inf,-inf);
	p[m++]=point(inf,-inf);
}
int dcmp(double x)
{
	if (fabs(x)<eps) return 0;
	return x<0?-1:1;
}
double cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}
point glt(point a,point a1,point b,point b1)
{
	vector v=a1-a; vector w=b1-b;
	vector u=a-b;
	double t=cross(w,u)/cross(v,w);
	return a+v*t;
}
void cut(point a,point b)
{
	int cnt=0;
	memset(tmp,0,sizeof(tmp));
	for (int i=0;i<m;i++){
		double c=cross(b-a,p[i]-a);
		double d=cross(b-a,p[(i+1)%m]-a);
		if (dcmp(c)<=0) tmp[cnt++]=p[i];
		if (dcmp(c)*dcmp(d)<0)
		 tmp[cnt++]=glt(a,b,p[i],p[(i+1)%m]);
	}
	m=cnt;
	for (int i=0;i<cnt;i++) p[i]=tmp[i];
}
vector rotate(vector a,double rad)
{
	return vector (a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
double len(vector v)
{
	return sqrt(v.x*v.x+v.y*v.y);
}
bool pd(double x)
{
	for (int i=2;i<=n+1;i++){
		vector v=b[i]-b[i-1];
		vector u=rotate(v,pi/2)/len(v);
		u=u*x;	
		line[i-1].a=v+u+b[i-1];
		line[i-1].b=line[i-1].a-v;
		int t=0;
	}
	init();
	for (int i=1;i<=n;i++){
		cut(line[i].a,line[i].b);
		if (!m) return true;
	}
	if (m==1) return true;
	return false;
}
int main()
{
	freopen("a.in","r",stdin);
	while (true){
		scanf("%d",&n);
		if (!n) break;
		for (int i=1;i<=n;i++) scanf("%lf%lf",&b[i].x,&b[i].y);
		b[n+1]=b[1];
		double l=0; double r=10000; double ans=0;
		while (r-l>eps){
			double mid=(l+r)/2;
			if (pd(mid)) ans=mid,r=mid-eps;
			else l=mid+eps;
		}
		printf("%.6lf\n",ans);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值