码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-31 17:40:04      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:struct   single   lsp   rom   rac   font   typedef   ini   scan   

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


题意:
给出一个形状为凸包的岛屿,求岛上离海(即凸包外)最远的点离海的距离有多远

二分多远,然后把凸包缩小这么远,看看此时半平面交有没有交集

注意这时候不是让你求每个点到了哪里,而是每条边到了哪里,所以用一个Line数组保存每条边到了哪里
求法向量时不要除长度了,等乘上mid后再除原长度,否则有精度问题
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=105;
const double INF=1e4+5;
const double eps=1e-10;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1; c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}
    return x*f;
}

inline int sgn(double x){
    if(abs(x)<eps) return 0;
    else return x<0?-1:1;
}

struct Vector{
    double x,y;
    Vector(double a=0,double b=0):x(a),y(b){}
    bool operator <(const Vector &a)const{
        return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0);
    }
    void print(){printf("%lf %lf\n",x,y);}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}
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 Len(Vector a){return sqrt(Dot(a,a));}
Vector Normal(Vector a){
    return Vector(-a.y,a.x);//counterClockwise
}
struct Line{
    Point s,t;
    Line(){}
    Line(Point a,Point b):s(a),t(b){}
};
bool isLSI(Line l1,Line l2){
    Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
    return sgn(Cross(v,u))!=sgn(Cross(v,w));
}
Point LI(Line a,Line b){
    Vector v=a.s-b.s,v1=a.t-a.s,v2=b.t-b.s;
    double t=Cross(v2,v)/Cross(v1,v2);
    return a.s+v1*t;
}

void iniPolygon(Point p[],int &n,double inf){
    n=0;
    p[++n]=Point(inf,inf);
    p[++n]=Point(inf,-inf);
    p[++n]=Point(-inf,-inf);
    p[++n]=Point(-inf,inf);
}
Point t[N];int tn;
void CutPolygon(Point p[],int &n,Point a,Point b){//get the left of a->b
    tn=0;
    Point c,d;
    for(int i=1;i<=n;i++){
        c=p[i],d=p[i%n+1];
        if(sgn(Cross(b-a,c-a))>=0) t[++tn]=c;
        if(isLSI(Line(a,b),Line(c,d)))
            t[++tn]=LI(Line(a,b),Line(c,d));
    }
    n=tn;for(int i=1;i<=n;i++) p[i]=t[i];
}
int n,m;
Point p[N],q[N];
Line L[N];
void ChangePolygon(Point p[],int n,double x){
    p[n+1]=p[1];
    for(int i=1;i<=n;i++){
        Vector v=Normal(p[i+1]-p[i])*x/Len(p[i+1]-p[i]);
        L[i]=Line(p[i]+v,p[i+1]+v);
    }
}
void solve(){
    double l=0,r=10000,e=1e-6;
    while(r-l>e){
        double mid=(l+r)/2;//printf("hi %lf %lf %lf\n",l,r,mid);
        ChangePolygon(p,n,mid);
        iniPolygon(q,m,INF);
        for(int i=1;i<=n;i++) CutPolygon(q,m,L[i].s,L[i].t);
        if(m) l=mid;
        else r=mid;
    }
    printf("%lf\n",l);
}

int main(int argc, const char * argv[]){
    while(true){
        n=read();if(n==0) break;
        for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        solve();
    }
}

 

 

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

标签:struct   single   lsp   rom   rac   font   typedef   ini   scan   

原文地址:http://www.cnblogs.com/candy99/p/6358939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!