POJ 3525 Most Distant Point from the Sea

 

Most Distant Point from the Sea
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3610   Accepted: 1667   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 nvertices. 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

Japan 2007

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
const int N=122;
const double eps=1e-9;
const double pi=acos(-1.0);
int dcmp(double a){if(abs(a)<eps)return 0;return a>0?1:-1;}
#define sig dcmp
double sqr(double a){return a*a;}
struct point
{
    double x,y;
    point(){}
    point(double _x,double _y){x=_x,y=_y;}
    void input(){cin>>x>>y;}
    double norm(){return sqrt(x*x+y*y);}
    point rot90(){return point(-y,x);}
    double abs(){return hypot(x,y);}
    bool operator==(point p){return !sig(x-p.x)&&!sig(y-p.y);}
    bool operator<(point p)const{
        if(!sig(y-p.y))return x<p.x;
        return y<p.y;
    }
};
double dist(point a,point b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
point operator+(point a,point b){return point(a.x+b.x,a.y+b.y);}
point operator-(point a,point b){return point(a.x-b.x,a.y-b.y);}
point operator*(point a,double b){return point(a.x*b,a.y*b);}
point operator*(double b,point a){return point(a.x*b,a.y*b);}
point operator/(point a,double b){return point(a.x/b,a.y/b);}
double det(point a,point b){return a.x*b.y-a.y*b.x;}
#define cross det
double dot(point a,point b){return a.x*b.x+a.y*b.y;}

struct line
{
    point a,b;
    line(){}
    line(point x,point y){a=x,b=y;}
};
bool onseg(point p,point s,point t)
{
    return dcmp(det(p-s,t-s))==0&&dcmp(dot(p-s,p-t))<0;
}
bool parallel(line a,line b)
{
    return dcmp(det(a.a-a.b,b.a-b.b))==0;
}
point rotate(point p,double a)
{
    double tx=p.x,ty=p.y;
    return point(tx*cos(a)-ty*sin(a),tx*sin(a)+ty*cos(a));
}
point make(line a,line b)
{
    double s1=det(a.a-a.b,b.b-b.a);
    double s2=det(a.b-b.a,b.b-b.a);
    point res=(s1*a.b-s2*a.a)/(s1-s2);
    return res;
}
line move_d(line a,double len)
{
    point d=a.b-a.a;
    d=d/d.norm();
    d=rotate(d,pi/2);
    return line(a.a+d*len,a.b+d*len);
}
#define Half line
#define half line
double arg(point a)
{
    return atan2(a.y,a.x);
}
bool ok(point a,Half p)
{
    return dcmp(det(a-p.a,p.b-p.a))<=0;
}
point crosspoint(Half a,Half b)
{
    double k=det(b.a-b.b,a.a-b.b);
    k=k/(k-det(b.a-b.b,a.b-b.b));
    return a.a+(a.b-a.a)*k;
}
bool cmp(Half a,half b)
{
    int res=dcmp(arg(a.b-a.a)-arg(b.b-b.a));
    return res==0?ok(a.a,b):res<0;
}
point p[N];
line v[N];
int n;
int halfp()
{
    sort(v,v+n,cmp);
    deque<half> q;
    deque<point> ans;
    q.push_back(v[0]);
    for(int i=1;i<n;i++)
    {
        if(dcmp(arg(v[i].b-v[i].a)-arg(v[i-1].b-v[i-1].a))==0)
            continue;
        while(ans.size()>0&&!ok(ans.back(),v[i]))
        {
            ans.pop_back();
            q.pop_back();
        }
        while(ans.size()>0&&!ok(ans.front(),v[i]))
        {
            ans.pop_front();
            q.pop_front();
        }
        ans.push_back(crosspoint(q.back(),v[i]));
        q.push_back(v[i]);
    }
    while(ans.size()>0&&!ok(ans.back(),q.front()))
    {
        ans.pop_back();
        q.pop_back();
    }
    while(ans.size()>0&&!ok(ans.front(),q.back()))
    {
        ans.pop_front();
        q.pop_front();
    }
    ans.push_back(crosspoint(q.back(),q.front()));
    return ans.size();
}
void solve()
{
    p[n]=p[0];
    double l=0,r=2e4;
    while(r-l>1e-7)
    {
        double mid=(l+r)/2;
        for(int i=0;i<n;i++)v[i]=move_d(line(p[i],p[i+1]),mid);
        int m=halfp();
        if(m<3)r=mid;else l=mid;
    }
    printf("%.6f\n",l);
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)p[i].input();
        solve();
    }
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
const int N=122;
const double eps=1e-9;
const double pi=acos(-1.0);
int dcmp(double t){return t<-eps?-1:t>eps;}
#define sig dcmp
#define Point point
double sqr(double a){return a*a;}
struct point
{
    double x,y;
    point(){}
    point(double _x,double _y){x=_x,y=_y;}
    void read(){scanf("%lf%lf",&x,&y);}
    void write(){printf("(%lf,%lf)\n",x,y);}
    point operator-(point p)const{return point(x-p.x,y-p.y);}
    point operator+(point p){return point(x+p.x,y+p.y);}
    point operator*(double t){return point(x*t,y*t);}
    point operator/(double t){return point(x/t,y/t);}
    point rot90(){return point(-y,x);}
    double abs(){return hypot(x,y);}
    bool operator==(point p){return !sig(x-p.x)&&!sig(y-p.y);}
    bool operator<(point p)const{
        if(!sig(y-p.y))return x<p.x;
        return y<p.y;
    }
};
double det(point a,point b){return a.x*b.y-a.y*b.x;}
#define cross det
point isSS(point a,point b,point c,point d)
{
    double l=det(a-c,b-c),r=-det(a-d,b-d);
    return  (c*r+d*l)/(r+l);
}
struct line
{
    point a,b;double ang;
    line(){}
    line(point _a,point _b)
    {a=_a,b=_b;;ang=atan2(b.y-a.y,b.x-a.x);}
    void set(point _a,point _b)
    {a=_a,b=_b;ang=atan2(b.y-a.y,b.x-a.x);}
    void write(){a.write();b.write();puts("");}
    bool operator==(line d)const{return !sig(ang-d.ang);}
    bool operator<(line d)const{
        if(sig(ang-d.ang))return ang<d.ang;
        return det(b-d.a,a-d.a)>0;
    }
};
#define Border line
point make(line a,line b)
{
    return isSS(a.a,a.b,b.a,b.b);
}
point isSS(line a,line b)
{
    return isSS(a.a,a.b,b.a,b.b);
}
int halfplane(line b[],int n,point hp[])
{
    static line q[N];
    sort(b,b+n);
    n=unique(b,b+n)-b;
    int l=0,r=0;
    for(int i=0;i<n;i++)
    {
        while(l+1<r&&sig(cross(b[i].b-b[i].a,isSS(q[r-1],q[r-2])-b[i].b))<0) r--;
        while(l+1<r&&sig(cross(b[i].b-b[i].a,isSS(q[l],q[l+1])-b[i].b))<0) l++;
        q[r++]=b[i];    ///q[r-1].write();
    }
    while(l+2<r&&sig(cross(q[l].b-q[l].a,isSS(q[r-1],q[r-2])-q[l].b))<0) r--;
    q[r]=q[l];
    int tot=0;
    for(int i=l;i<r;i++)hp[tot++]=isSS(q[i],q[i+1]);//,hp[tot-1].write();
    return tot;
}
point p[N],hp[N];
line b[N];
int n;

void solve()
{
    double l=0,r=2e4;   //int z=0;
    double low = 0, high = 20000;
        while(high - low > 1e-7) {
            double mid = (high + low) / 2;
            for(int i=0;i<n;++i) {
                Point t = (p[i+1] - p[i]).rot90();
                t = t*(mid/t.abs());
                b[i].set(p[i]+t, p[i+1]+t);
            }
            int tot = halfplane(b, n, hp);
            if(tot < 3) high = mid;
            else low = mid;
        }
        printf("%.6f\n", low);
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)p[i].read();p[n]=p[0];
        solve();
    }
}


你可能感兴趣的:(Algorithm,数据结构,编程,算法,poj)