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

参考:http://blog.csdn.net/FXXKI/article/details/45674979
看完别人写的发现思路还是挺简单的

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 110;
const double INF = 1e8;
const double eps = 1e-8;
struct Point
{
    double x,y;
    Point() {}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
};

Point ori[MAXN];
Point np[MAXN];
Point tp[MAXN];
int n,m;

Point Intersection(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 t;
    t.x = (p1.x*v + p2.x*u)/(u+v);
    t.y = (p1.y*v + p2.y*u)/(u+v);
    return t;
}

void Get_equation(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 - p1.x*p2.y;
}

void Cut(double a,double b,double c)
{
    int tmp = 0;
    for(int i = 1; i <= m; i++)
    {
        if(a*np[i].x + b*np[i].y + c < eps)
            tp[++tmp] = np[i];
        else
        {
            if(a*np[i-1].x + b*np[i-1].y + c < -eps)
                tp[++tmp] = Intersection(np[i-1],np[i],a,b,c);
            if(a*np[i+1].x + b*np[i+1].y + c  < -eps)
                tp[++tmp] = Intersection(np[i],np[i+1],a,b,c);
        }
    }
    for(int i = 1; i <= tmp; i++)
        np[i] = tp[i];
    np[0] = np[tmp];
    np[tmp+1] = np[1];
    m = tmp;
}

bool C(double d)
{
    double a,b,c;
    for(int i = 0; i <= n+1; ++i)
        np[i] = ori[i];
    m = n;
    for(int i = 1; i <= n; ++i)
    {
        Get_equation(ori[i],ori[i+1],a,b,c);
        c += d*sqrt(a*a+b*b);
        Cut(a,b,c);
    }
    return m > 0;
}

double solve()
{
    double lb = 0,ub = INF;
    while(fabs(ub-lb) > 1e-5)
    {
        double mid = (ub+lb)/2;
        if(C(mid)) lb = mid;
        else ub = mid;
    }
    return ub;
}

int main()
{
    while(scanf("%d",&n) && n)
    {
        for(int i = 1; i <= n; ++i)
            scanf("%lf %lf",&ori[i].x,&ori[i].y);
        ori[0] = ori[n];
        ori[n+1] = ori[1];
        double res = solve();
        printf("%f\n",res);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值