您的位置:首页 > 其它

POJ 3525 Most Distant Point from the Sea 半平面交+二分

2015-04-11 23:20 435 查看
半平面交+二分

二分距离,判断是否有半平面交,精度要求很高

Most Distant Point from the Sea

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4163 Accepted: 1942 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 (xi, yi)–(xi+1, yi+1)
(1 ≤ i ≤ n − 1) and the line segment (xn, yn)–(x1, y1) 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

Japan 2007
[Submit]   [Go Back]   [Status]  
[Discuss]

/* ***********************************************
Author        :CKboss
Created Time  :2015年04月09日 星期四 19时43分00秒
File Name     :POJ1279.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

#define mp make_pair
#define pb push_back

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = 1e9;
const int maxp = 2111;

int dcmp(double d) { if(fabs(d)<eps) return 0; return (d<0)?-1:1; }
inline double sqr(double x) { return x*x; }

struct point
{
double x,y;
point(double _x=0,double _y=0):x(_x),y(_y){}
void input() { scanf("%lf%lf",&x,&y); }
void output() { printf("%.2lf %.2lf\n",x,y); }
bool operator==(point a) const { return dcmp(a.x-x)==0&&dcmp(a.y-y)==0; }
bool operator<(point a) const { return dcmp(a.x-x)==0?dcmp(y-a.y)<0:x<a.x; }
double len() { return hypot(x,y); }
double len2() { return x*x+y*y; }
double distance(point p) { return hypot(x-p.x,y-p.y); }
point add(point p) { return point(x+p.x,y+p.y); }
point sub(point p) { return point(x-p.x,y-p.y); }
point mul(double b) { return point(x*b,y*b); }
point div(double b) { return point(x/b,y/b); }
double dot(point p) { return x*p.x+y*p.y; }
double det(point p) { return x*p.y-y*p.x; }
double rad(point a,point b)
{
point p=*this;
return fabs(atan2(fabs(a.sub(p).det(b.sub(p))),a.sub(p).dot(b.sub(p))));
}
//单位向量
point horunit(){return this->div(this->len());}
//左转的单位法向量
point verunit(){return point(-y,x).div(this->len());}
//向向量v的方向移动距离d
point todir(point v,double d)
{
return point(x+v.x*d,y+v.y*d);
}
};

struct line
{
point a,b;
line(){}
line(point _a,point _b) { a=_a; b=_b; }
bool operator==(const line v) const { return (a==v.a)&&(b==v.b); }
bool parallel(line v) { return dcmp(b.sub(a).det(v.b.sub(v.a)))==0; }
point crosspoint(line v)
{
double a1=v.b.sub(v.a).det(a.sub(v.a));
double a2=v.b.sub(v.a).det(b.sub(v.a));
return point((a.x*a2-b.x*a1)/(a2-a1),(a.y*a2-b.y*a1)/(a2-a1));
}

void adjust()
{
if (b<a)swap(a,b);
}

/// a-->b向左垂直方向平移距离d
line transhor(double d)
{
point v=b.sub(a);
point dir=v.verunit();
a=a.todir(dir,d);
b=b.todir(dir,d);
return *this;
}

void output()
{
printf("(%.2lf,%.2lf) --- (%.2lf,%.2lf)\n",a.x,a.y,b.x,b.y);
}
};

struct polygon
{
int n;
point p[maxp];
line l[maxp];

double getarea()
{
double sum=0;
int i;
for(i=0;i<n;i++)
{
sum+=p[i].det(p[(i+1)%n]);
}
return fabs(sum)/2;
}
};

struct halfplane:public line
{
double angle;
halfplane(){}
/// a-->b left
halfplane(point _a,point _b){ a=_a; b=_b;}
halfplane(line v) { a=v.a; b=v.b; }
void calcangle() { angle=atan2(b.y-a.y,b.x-a.x); }
bool operator<(const halfplane &b) const { return angle<b.angle; }
};

struct halfplanes
{
int n;
halfplane hp[maxp];
point p[maxp];
int que[maxp];
int st,ed;
void push(halfplane tmp)
{
hp[n++]=tmp;
}
void unique()
{
int m=1,i;
for(i=1;i<n;i++)
{
if(dcmp(hp[i].angle-hp[i-1].angle)) hp[m++]=hp[i];
else if(dcmp(hp[m-1].b.sub(hp[m-1].a).det(hp[i].a.sub(hp[m-1].a))>0))
hp[m-1]=hp[i];
}
n=m;
}
bool halfplaneinsert()
{
int i;
for(int i=0;i<n;i++) hp[i].calcangle();
sort(hp,hp+n);
unique();
que[st=0]=0; que[ed=1]=1;
p[1]=hp[0].crosspoint(hp[1]);
for(i=2;i<n;i++)
{
while(st<ed&&dcmp((hp[i].b.sub(hp[i].a).det(p[ed].sub(hp[i].a))))<0) ed--;
while(st<ed&&dcmp((hp[i].b.sub(hp[i].a).det(p[st+1].sub(hp[i].a))))<0) st++;
que[++ed]=i;
if(hp[i].parallel(hp[que[ed-1]])) return false;
p[ed]=hp[i].crosspoint(hp[que[ed-1]]);
}
while(st<ed&&dcmp((hp[st].b.sub(hp[que[st]].a).det(p[ed].sub(hp[que[st]].a))))<0) ed--;
while(st<ed&&dcmp((hp[que[ed]].b.sub(hp[que[ed]].a).det(p[st+1].sub(hp[que[ed]].a))))<0) st++;
if(st+1>=ed) return false;
return true;
}

void getconvex(polygon &con)
{
p[st]=hp[que[st]].crosspoint(hp[que[ed]]);
con.n=ed-st+1;
int j=st,i=0;
for(;j<=ed;i++,j++) con.p[i]=p[j];
}
};

int n;
vector<point> vp;

bool check(double d)
{
halfplanes hps ;
hps.n=0;
for(int i=0;i<n;i++)
{
/// i-->i+1
line l(vp[i],vp[(i+1)%n]);
l = l.transhor(d);
hps.push(halfplane(l));
}
return hps.halfplaneinsert();
}

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

while(scanf("%d",&n)!=EOF&&n)
{
vp.clear();
for(int i=0;i<n;i++)
{
point tp; tp.input(); vp.pb(tp);
}

double low=0,high=inf,ans=inf;
while(low+eps<high)
{
double mid = (low+high)/2;
if(check(mid))
{
ans=mid; low = mid;
}
else high=mid;
}

printf("%.6f\n",ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: