POJ 3525/LA 3890 二分+半平面交

Most Distant Point from the Sea

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

【解题报告】
蓝书上的题。。。
POJ上好像要卡常数啊
LA上就过了

代码如下:

#include<algorithm>  
#include<vector>   
#include<deque>  
#include<cstdio>  
#include<cstring>  
#include<cmath>  
using namespace std;  
#define INF 1e9
#define eps 1e-8

struct Point
{  
    double x,y;         
    Point(double _ = .0,double __ = .0):x(_),y(__) {}  
    friend bool operator <(const Point &a,const Point &b)
    {return a.x<b.x||(a.x==b.y&&a.y<b.y);}
    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);}
};
typedef Point Vector; 
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}  
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}  
double Length(Vector a){return sqrt(Dot(a,a));}  
Vector Normal(Vector a){double L=Length(a);return Vector(-a.y/L,a.x/L);}  

long double PolygonArea(vector<Point>p)  
{  
    int n=p.size();  
    double area=0;  
    for(int i=1;i<n-1;i++)  
        area+=Cross(p[i]-p[0],p[i+1]-p[0]);  
    return area/2;  
}  

struct Line  
{  
    Point p;  
    Vector v;  
   double ang;  
    Line(){}  
    Line(Point p,Vector v):p(p),v(v){ang=atan2(v.y,v.x);}  
    bool operator<(const Line&L)const  
    {return ang<L.ang;}  
};  

bool OnLeft(Line L,Point p)  
{  
    return Cross(L.v,p-L.p)>0;  
}  
Point GetLineIntersection(Line a,Line b)  
{  
    Vector u=a.p-b.p;  
    double t=Cross(b.v,u)/Cross(a.v,b.v);  
    return a.p+a.v*t;  
}  
vector<Point> HalfplaneIntersection(vector<Line>L)  
{  
    int n=L.size();  
    sort(L.begin(),L.end());  
    int first,last;  
    vector<Point> p(n);  
    vector<Line> q(n);  
    vector<Point> ans;  
    q[first=last=0]=L[0];  
    for(int i=1;i<n;i++)  
    {  
        while(first<last&&!OnLeft(L[i],p[last-1]))last--;  
        while(first<last&&!OnLeft(L[i],p[first]))first++;  
        q[++last]=L[i];  
        if(fabs(Cross(q[last].v,q[last-1].v))<eps)  
        {  
            last--;  
            if(OnLeft(q[last],L[i].p))q[last]=L[i];  
        }  
        if(first<last)p[last-1]=GetLineIntersection(q[last-1],q[last]);  
    }  
    while(first<last&&!OnLeft(q[first],p[last-1]))last--;  
    if(last-first<=1) return ans;  
    p[last]=GetLineIntersection(q[last],q[first]);  
    for(int i=first;i<=last;i++)ans.push_back(p[i]);  
    return ans;  
}  
int main()  
{  
    int n;  
    while(~scanf("%d",&n)&&n)  
    {  
        vector<Point>p,v,normal;  
        int m,x,y;  
        for(int i=0;i<n;i++)  
        {  
            scanf("%d%d",&x,&y);  
            p.push_back(Point(x,y));  
        }  
        if(PolygonArea(p)<0)reverse(p.begin(),p.end());//如果面积为负,说明是顺时针输入的,需要翻转数组  
        for(int i=0;i<n;i++)  
        { 
            v.push_back(p[(i+1)%n]-p[i]);  
            normal.push_back(Normal(v[i]));  
        }  
        double LL=0,RR=5010;  
        while(RR-LL>eps)  
        {  
            vector<Line>L;  
            double mid=LL+(RR-LL)/2;  
            for(int i=0;i<n;i++)  
                L.push_back(Line(p[i]+normal[i]*mid,v[i]));  
            vector<Point>poly=HalfplaneIntersection(L);  
            if(poly.empty())RR=mid;  
            else LL=mid;  
        }  
        printf("%.6lf\n",LL);  
    }  
    return 0;
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值