题目链接:http://poj.org/problem?id=3608
方法:凸包+旋转卡壳
永远不会忘了这道题,WA+TLE30来次 恶心啊
最后还来个G++提交 %lf错误 %f正确
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 10000
const double eps = 1e-10;
//定义点
struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector 为 Point的别名
Point p[N+10],q[N+10],t[N+10];
int np, nq;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
//点-点=向量
Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}
bool operator == (const Point &a, const Point &b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
//点积:两者长度乘积再乘上夹角余弦 XaXb + YaYb
double Dot(Vector A, Vector B){return A.x*B.x + A.y*B.y;}
double Length(Vector A){return sqrt(Dot(A, A));}
double Angle(Vector A, Vector B){return acos(Dot(A, B) / Length(A) / Length(B));}
//叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa
double Cross(Vector A, Vector B){return A.x*B.y - A.y*B.x;}
double Area2(Point A, Point B, Point C){return Cross(B-A, C-A);}
bool cmp ( Point a, Point b )
{
if ( a.x != b.x ) return a.x < b.x;
else return a.y < b.y;
}
int ConvexHull(Point *p, int n, Point * ch)
{
sort(p, p+n, cmp); //先比较x坐标,在比较y坐标
int m = 0;
//上凸边
for(int i = 0; i < n; i++)
{
while(m > 1 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
//下凸边
for(int i = n-2; i >= 0; i--)
{
while(m > k && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
return m;
}
//点到线段的距离
double DistanceToSegment(Point P, Point A, Point B)
{
if(A == B) return Length(P-A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
//P点的投影不在线段上
if(dcmp(Dot(v1, v2)) < 0) return Length(v2);//P点在A点的左上方
else if(dcmp(Dot(v1, v3)) > 0) return Length(v3); //P点在B点的右上方
//P点的投影在线段上
else return fabs(Cross(v1, v2)) / Length(v1);
}
double Get_angle(Point a1, Point b1, Point a2, Point b2)
{
Point t;
t.x=a2.x-(b2.x-a1.x);
t.y=a2.y-(b2.y-a1.y);
return Cross(t-a1,b1-a1);
}
double slove()
{
int yminP = 0, ymaxQ = 0;
for(int i = 0; i < np; i++)
if(p[i].y < p[yminP].y ||(p[i].y==p[yminP].y&&(p[i].x<p[yminP].x)))
yminP = i;
for(int i = 0; i < nq; i++)
if(q[i].y > q[ymaxQ].y ||(q[i].y==q[ymaxQ].y&&(q[i].x>q[ymaxQ].x)))
ymaxQ = i;
p[np] = p[0];
q[nq] = q[0];
double ans = Length(p[yminP]-q[ymaxQ]);
int rep = yminP, req = ymaxQ;
do
{
double angle = Get_angle(p[yminP],p[yminP+1],q[ymaxQ],q[ymaxQ+1]);
//double tmpp = Angle((p[((yminP-1)+np)%np]-p[yminP]),(p[yminP+1]-p[yminP]));
//double tmpq = Angle((q[((ymaxQ-1)+nq)%nq]-q[ymaxQ]),(q[ymaxQ+1]-q[ymaxQ]));
if(dcmp(angle) == 0)
{
ans = min(ans, DistanceToSegment(p[yminP],q[ymaxQ],q[ymaxQ+1]));
ans = min(ans, DistanceToSegment(p[yminP+1],q[ymaxQ],q[ymaxQ+1]));
ans = min(ans, DistanceToSegment(q[ymaxQ],p[yminP],p[yminP+1]));
ans = min(ans, DistanceToSegment(q[ymaxQ+1],p[yminP],p[yminP+1]));
yminP = (yminP+1)%np;
ymaxQ = (ymaxQ+1)%nq;
}
else
{
if(dcmp(angle) < 0)
{
ans = min(ans, DistanceToSegment(q[ymaxQ],p[yminP],p[yminP+1]));
yminP = (yminP+1)%np;
}
else
{
ans = min(ans, DistanceToSegment(p[yminP],q[ymaxQ],q[ymaxQ+1]));
ymaxQ = (ymaxQ+1)%nq;
}
}
}while(!(rep==yminP&&req==ymaxQ));
return ans;
}
int main ()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
if(n == 0 && m == 0) break;
for(int i = 0; i < n; i++)
scanf("%lf %lf", &t[i].x, &t[i].y);
np = ConvexHull(t,n,p);
for(int i = 0; i < m; i++)
scanf("%lf %lf", &t[i].x, &t[i].y);
nq = ConvexHull(t,m,q);
double ans = slove();
printf("%.5f\n",ans);
}
return 0;
}