POJ 3525 Most Distant Point from the Sea 半平面交判多边形存在

问凸多边形中某点到边界距离最远有多大。

最优值问题转化判定性问题。

判定就很好判定啦,将凸多边形往内缩答案,如果半平面交有解那么答案就是允许的。

#define FOR(i,j,k) for(i=j;i<=k;i++)
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N = 20005;
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); }
    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; }
    Point operator *(double b) const { return Point(b * x, b * y); }
    Point normal() { double l = sqrt(x * x + y * y); return Point(-y / l, x / l); }
} p[N], poly[N];

struct Line {
    Point x, v, n;
    double ang;
    Line(){}
    Line(const Point &a, const Point &b) : x(a) {
        v = b - a; n = v.normal();
        ang = atan2(v.y, v.x);
    }
    
    bool operator <(const Line &b) const {
        if (ang == b.ang) return v * (b.x - x) < 0;
        return ang < b.ang;
    }
    
    bool operator ==(const Line &b) const { return ang == b.ang; }
    
    Point intersection(const Line &b) {
        Point u = x - b.x;
        double t = (b.v * u) / (v * b.v);
        return x + v * t;
    }

    void moveAlongNormal(double multiple) {
        x = x + n * multiple;
    }
} lines[N];

bool right(const Point &p, const Line &l) {
    return l.v * (p - l.x) < 0;
}

int half_plane_intersection(Line l[], int n, Point poly[]) {
    static Line q[N];
    sort(l + 1, l + n + 1);
    n = unique(l + 1, l + n + 1) - (l + 1);
    int f = 1, r = 0, m = 0;
    q[++r] = l[1]; q[++r] = l[2];
    for (int i = 3; i <= n; i++) {
        while (f < r && right(q[r].intersection(q[r - 1]), l[i])) r--;
        while (f < r && right(q[f].intersection(q[f + 1]), l[i])) f++;
        q[++r] = l[i];
    }
    while (f < r && right(q[r].intersection(q[r - 1]), q[f])) r--;
    while (f < r && right(q[f].intersection(q[f + 1]), q[r])) f++;
    q[r + 1] = q[f];
    for (int i = f; i <= r; i++)
        poly[++m] = q[i].intersection(q[i + 1]);
    return m;
}

int main() {
    int n, i, j;
    while(scanf("%d", &n) != EOF && n) {
        FOR(i,1,n) scanf("%lf%lf", &poly[i].x, &poly[i].y);
        poly[n + 1] = poly[1];
        FOR(i,1,n) lines[i] = Line(poly[i], poly[i + 1]);
        double l = 0, r = 20000;
        while (r - l > 1e-7) {
            double mid = (l + r) / 2;
            FOR(i,1,n) lines[i].moveAlongNormal(mid);
            int m = half_plane_intersection(lines, n, poly);
            if (m > 2) l = mid; else r = mid;
            FOR(i,1,n) lines[i].moveAlongNormal(-mid);
        }
        printf("%.6lf\n", l);
    }
    return 0;
}

Most Distant Point from the Sea
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4462 Accepted: 2073 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值