UVAlive 3890&Poj3525 半平面交+二分 解题报告

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

思路

半平面交+二分即可
来啊套板子啊!!!

代码

我的rt有点小,虽然不影响。。。

#include  
#include  
#include  
#include  
#include 
#include 
using namespace std;  
const double eps=1e-8;
const int M=100+5;
struct point
{  
    double x,y;  
};
point A[M],p[M],q[M];  
int N,tempcnt,endcnt;  
void guizheng()
{  
    for (int i=1;i<=N;++i)
    q[i]=A[N-i+1];  
    for (int i=1;i<=N;++i)  
    A[i]=q[i];   
}  
void getline(point p1,point p2,double &a,double &b,double &c)
{  
    a=p2.y-p1.y;  
    b=p1.x-p2.x;  
    c=p2.x*p1.y-p2.y*p1.x;  
}  
void init()
{  
    for (int i=1;i<=N;++i)
    p[i]=A[i];      
    p[N+1]=p[1];p[0]=p[N];  
    endcnt=N;  
}  
point getinter(point p1,point p2,double a,double b,double c)
{  
    double u=fabs(a*p1.x+b*p1.y+c);  
    double v=fabs(a*p2.x+b*p2.y+c);  
    point temp;  
    temp.x=(v*p1.x+u*p2.x)/(u+v);  
    temp.y=(v*p1.y+u*p2.y)/(u+v);  
    return temp;  
}  
void cut(double a,double b,double c)
{  
    tempcnt=0;  
    for (int i=1;i<=endcnt;++i)
    {  
        if (a*p[i].x+b*p[i].y+c>=0) q[++tempcnt]=p[i];  
        else
        {  
            if (a*p[i-1].x+b*p[i-1].y+c>0)
            q[++tempcnt]=getinter(p[i],p[i-1],a,b,c);  
            if (a*p[i+1].x+b*p[i+1].y+c>0)  
            q[++tempcnt]=getinter(p[i],p[i+1],a,b,c);  
        }  
    }  
    for (int i=1;i<=tempcnt;++i)  
    p[i]=q[i];  
    p[tempcnt+1]=q[1];p[0]=p[tempcnt];  
    endcnt=tempcnt;  
}  
bool solve(double r)
{  
    init();  
    for (int i=1;i<=N;++i)
    {  
        point ta,tb,tc;  
        tc.x=A[i+1].y-A[i].y;tc.y=A[i].x-A[i+1].x;  
        double k=r/sqrt(tc.x*tc.x+tc.y*tc.y);  
        tc.x*=k;tc.y*=k;  
        ta.x=A[i].x+tc.x;ta.y=A[i].y+tc.y;  
        tb.x=A[i+1].x+tc.x;tb.y=A[i+1].y+tc.y;  
        double a,b,c;  
        getline(ta,tb,a,b,c);  
        cut(a,b,c);  
    }  
    if (endcnt<=0) return false;  
    return true;  
}  
int main()  
{  
    freopen("island.in","r",stdin);
    freopen("island.out","w",stdout);
    int i,j,k;  
    scanf("%d",&N);
    for(i=1;i<=N;++i)
    scanf("%lf%lf",&A[i].x,&A[i].y);  
    double left=0,right=1000;  
    guizheng();  
    A[N+1]=A[1];  
    while(left+eps<=right)
    {  
        double mid=(left+right)/2.0;  
        if (solve(mid)) left=mid;  
        else right=mid;  
    }  
    printf("%.2lf\n",right);  
    return 0;  
}  

你可能感兴趣的:(————单个题目———,————计算几何————)