Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 5748 | Accepted: 2550 | 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 (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
本题大意:给定一个凸多边形,若定义一个多边形内的点到多边形的距离为:所有多边形的边中和它相距最近的边到它的距离,求多边形内距离最远的点。
最大值最小,想到可以二分,那么怎么验证答案呢?我们想到:假设这个距离为x,那么我们把多边形的每一条边所在的有向直线(方向看凸包顺序),向内移动x的距离,最终一定会围成一个面积为0的图形。如果我们二分的答案比x小,面积将不为零。如果答案比x大,那么将围不成图形。我们用半平面交来做,判断有无解即可。
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int maxn = 1e3 + 5; namespace G2D { #define T double #define eps 1e-11 struct P {T x, y;P(T _x, T _y):x(_x), y(_y){}P(){}}; inline int sg(T a) {return (a > -eps) - (a < eps);} bool cmp_x(const P &A, const P &B) {return A.x == B.x ? A.y < B.y : A.x < B.x;} inline P operator +(const P &A, const P &B) {return P(A.x + B.x, A.y + B.y);} inline P operator -(const P &A, const P &B) {return P(A.x - B.x, A.y - B.y);} inline P operator -(const P &A) {return P(-A.x, -A.y);} inline T operator *(const P &A, const P &B) {return A.x * B.y - B.x * A.y;} inline P operator *(const T &A, const P &B) {return P(A * B.x, A * B.y);} inline int AOnLeft(const P &A, const P &B) {return sg(B * A);} /*不要乘反,右手定则*/ inline int AOnLeft(const P &A, const P &s, const P &t) {return sg((s - A) * (t - A));} /*不要乘反,右手定则*/ inline T Gdis(const P &A, const P &B) {return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));} struct Line { P s, t; double a;Line(){} Line(P _s, P _t):s(_s), t(_t){a = atan2((_t - _s).x, (_t - _s).y);} }q[maxn << 1]; inline bool operator <(const Line &A, const Line &B) {return A.a < B.a;} inline P Itsec(const Line &A, const Line &B) { T s1 = ((B.t - A.s) * (B.s - A.s)), s2 = ((B.s - A.t) * (B.t - A.t)); return (s1 / (s1 + s2)) * (A.t - A.s) + A.s; } inline T GetArea(P * c, int cnt) { T ans = 0; for(register int i = 1; i < cnt; ++i) ans += (c[i] - c[0]) * (c[i + 1] - c[0]); return -ans; } void GetConvex(P * c, int & cnt, P * pset, int size) { sort(pset + 1, pset + size + 1, cmp_x); cnt = 0, c[++cnt] = pset[1], c[++cnt] = pset[2]; for(register int i = 3; i <= size; ++i) { while(cnt > 1 && AOnLeft(pset[i] - c[cnt], c[cnt] - c[cnt - 1]) <= 0) --cnt; c[++cnt] = pset[i]; }/*记得限制不能返回去*/ int k = cnt; for(register int i = size - 1; i >= 1; --i) { while(cnt > k && AOnLeft(pset[i] - c[cnt], c[cnt] - c[cnt - 1]) <= 0) --cnt; c[++cnt] = pset[i]; } } bool HPI(Line * lset, int size, P * convex, int & tot) { int L = 1, R = 0, cnt = 0;tot = 0; q[++R] = lset[1], q[++R] = lset[2]; for(register int i = 3; i <= size; ++i) { while(L < R && AOnLeft(Itsec(q[R], q[R - 1]), lset[i].s, lset[i].t) <= 0) --R; while(L < R && AOnLeft(Itsec(q[L], q[L + 1]), lset[i].s, lset[i].t) <= 0) ++L; q[++R] = lset[i]; } while(L < R && AOnLeft(Itsec(q[R], q[R - 1]), q[L].s, q[L].t) <= 0) --R; while(L < R && AOnLeft(Itsec(q[L], q[L + 1]), q[R].s, q[R].t) <= 0) ++L; if(R - L <= 1) return 0; for(register int i = L; i < R; ++i) convex[++tot] = Itsec(q[i], q[i + 1]); convex[++tot] = Itsec(q[L], q[R]); convex[++tot] = convex[1]; return 1; } } using namespace G2D; P convex[maxn], last, now, bg; Line lset[maxn], nlset[maxn]; int size, cnt; void GetSrk(double lth) { P mv, ar; double Agl; for(register int i = 1; i <= size; ++i) { ar = lset[i].t - lset[i].s; mv.x = -ar.y, mv.y = ar.x; Agl = sqrt(mv.x * mv.x + mv.y * mv.y); mv = (1.0 / Agl) * mv; nlset[i].s = lset[i].s + lth * mv; nlset[i].t = lset[i].t + lth * mv; } } double DF(double L, double R) { int stu; while(R - L >= 1e-7) { double mid = (L + R) / 2.0; GetSrk(mid), stu = HPI(nlset, size, convex, cnt); if(!stu) R = mid; else L = mid; } return L; } int main() { while(1) { scanf("%d", &size); if(!size) break; scanf("%lf %lf", &last.x, &last.y), bg = last; for(register int i = 2; i <= size; ++i) { scanf("%lf %lf", &now.x, &now.y); lset[i - 1] = Line(last, now); last = now; } lset[size] = Line(now, bg); sort(lset + 1, lset + size + 1); printf("%lf\n", DF(0, 5002)); } return 0; }
没有帐号? 立即注册