链接:http://acm.hdu.edu.cn/showproblem.php?pid=4667
题意:给n个圆m个三角形,求包住它们所需的最小长度。
思路:比赛的时候只想到了三角形用凸包围一下,圆不知道怎么处理。暴力一点的方法呢,把圆均分成了2000个整点,然后求凸包,圆弧上的弧线也用折线替代,这样对精度有损,一开始分成1000个点的时候精度就不够,wa掉了,如果圆上的弧线还是算弧长的话,可能还是可以过的。解题报告上说的是求任意两圆的外切线,得到所有切点,三角形顶点和圆的切点,三角形的顶点来求凸包。
暴力:
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int maxn=200000; const double eps=1e-10; const double PI=acos(-1.0); struct Point { double x,y; Point(double x=0,double y=0):x(x),y(y) {} bool operator < (const Point& a) const { if(a.x != x) return x < a.x; return y < a.y; } }; typedef Point Vector; Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); } Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y-B.y); } Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); } int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } bool operator == (const Point& a,const Point& b) { return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0; } 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 Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } double dist(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int ConvexHull(Point *p,int n,Point *ch) { sort(p,p+n); n=unique(p,p+n)-p; int m=0; for(int i=0; i<n; i++) { while(m>1 && 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 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--; ch[m++]=p[i]; } if(n>1) m--; return m; } Point p[maxn],ch[maxn]; int num=2000; int main() { freopen("1002.in","r",stdin); int n,m; while(~scanf("%d%d",&n,&m)) { double x,y,r; int cnt=0; while(n--) { scanf("%lf%lf%lf",&x,&y,&r); for(int i=0;i<num;i++) p[cnt++]=Point((x+r*cos(2*PI*i/num)),(y+r*sin(2*PI*i/num))); } while(m--) { for(int i=0;i<3;i++) { scanf("%lf%lf",&x,&y); p[cnt++]=Point(x,y); } } int k=ConvexHull(p,cnt,ch); double ans=0; for(int i=0;i<k;i++) ans+=dist(ch[i],ch[(i+1)%k]); printf("%.10lf\n",ans); } return 0; }