现在的位置: 首页 > 综合 > 正文

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

2014年09月29日 ⁄ 综合 ⁄ 共 3641字 ⁄ 字号 评论关闭
Most Distant Point from the Sea
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3647   Accepted: 1688   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

题意:等价于,给定一个凸多边形,求里面放的最大的圆的半径
题解:竟然1A了。。。二分半径r,将所有边都向内推进r,判断是否存在半平面交,二分的精度控制在1e-6就可以了
#include<stdio.h>
#include<math.h>
#define eps 1e-6
struct point{
    double x,y;
}p[108],tp[208],temp[208];
int n;
double a,b,c;
double cross(struct point p1,struct point p2,struct point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
void make_clock()
{
    double area=0;
    int i;

    for(i=2;i<=n;i++) area+=cross(p[1],p[i],p[i+1]);
    if(area>0)
    {
        for(i=0;i<=n+1-i;i++)
        {
            p[103]=p[i];
            p[i]=p[n+1-i];
            p[n+1-i]=p[103];
        }
    }
}
void move_get_line(struct point p1,struct point p2,double r)
{
    struct point tmd;
    double len;

    tmd.x=p2.y-p1.y;
    tmd.y=p1.x-p2.x;
    len=sqrt(tmd.x*tmd.x+tmd.y*tmd.y);
    tmd.x=tmd.x/len*r;
    tmd.y=tmd.y/len*r;
    p1.x+=tmd.x,p1.y+=tmd.y;
    p2.x+=tmd.x,p2.y+=tmd.y;
    a=p1.y-p2.y;
    b=p2.x-p1.x;
    c=p1.x*p2.y-p1.y*p2.x;
}
void get_intersect(struct point p1,struct point p2)
{
    double a2=p1.y-p2.y;
    double b2=p2.x-p1.x;
    double c2=p1.x*p2.y-p1.y*p2.x;
    double tmd=a*b2-a2*b;
    p[103].x=(b*c2-b2*c)/tmd;
    p[103].y=(a2*c-a*c2)/tmd;
}
int cut(int m)
{
    int top=0,i;

    for(i=1;i<=m;i++)
    {
        if(a*temp[i].x+b*temp[i].y+c<=0) tp[++top]=temp[i];
        else
        {
            if(a*temp[i-1].x+b*temp[i-1].y+c<0)
            {
                get_intersect(temp[i-1],temp[i]);
                tp[++top]=p[103];
            }
            if(a*temp[i+1].x+b*temp[i+1].y+c<0)
            {
                get_intersect(temp[i+1],temp[i]);
                tp[++top]=p[103];
            }
        }
    }
    tp[0]=tp[top],tp[top+1]=tp[1];
    for(i=0;i<=top+1;i++) temp[i]=tp[i];

    return top;
}
int half_plane(double r)
{
    int m=n,i;

    for(i=0;i<=n+1;i++) temp[i]=p[i];
    for(i=1;i<=n;i++)
    {
        move_get_line(p[i],p[i+1],r);
        m=cut(m);
    }

    return m;
}
double solve()
{
    double left=0,right=100000000,mid;

    while(left+eps<right)
    {
        mid=(left+right)/2.0;
        if(half_plane(mid)) left=mid;
        else right=mid;
    }

    return left;
}
int main()
{
    int i;

    //freopen("t.txt","r",stdin);
    while(scanf("%d",&n)&&n)
    {
        for(i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        p[0]=p[n],p[n+1]=p[1];
        make_clock();
        printf("%lf\n",solve());
    }

    return 0;
}

抱歉!评论已关闭.