题目读起来挺纠结的,题意的大概是:若给定点在凸包外面的话,求该点到凸包的最短距离,在凸包内的话,距离为0。
思路:首先判断该点是否在凸包外面,利用叉积;然后搜索点到每条线短的最短距离,不断更新。
bool inconvex()
{
int i;
FOR(i, 0, n)
if(direction(point[i],point[i+1]) > 0)
return false;
return true;
}
double getdis(Point p0, Point p2)
{
return sqrt(((p0.x-p2.x)*(p0.x-p2.x)+(p0.y-p2.y)*(p0.y-p2.y))*1.0);
}
double getr(Point p0, Point p2)
{
double a, b, c, s;
a = getdis(p0, p);
b = getdis(p2, p);
c = getdis(p0, p2);
s = fabs(direction(p0, p2)*1.0/2);
if (s < eps) {
int minx, miny, maxx, maxy;
minx = min(p0.x, p2.x);
maxx = max(p0.x, p2.x);
miny = min(p0.y, p2.y);
maxy = max(p0.y, p2.y);
if (p.x>=minx && p.x<=maxx && p.y>=miny &&p.y<=maxy)
return 0;
else
return min(a, b);
}
if (a*a >= b*b + c*c)
return b;
else if (b*b >= a*a + c*c)
return a;
else
return 2*s*1.0/c;
}
int main()
{
scanf("%d %d %d", &p.x, &p.y, &n);
int i;
FOR(i, 0, n)
scanf("%d %d", &point[i].x, &point[i].y);
point[n] = point[0];
if (inconvex())
printf("0.000\n");
else {
double ans = INF, d;
FOR(i, 0, n) {
d = getr(point[i], point[i+1])*2;
if (d < ans)
ans = d;
}
printf("%.3lf\n", ans);
}
return 0;
}