sgu 332 Largest Circle 求凸n边形内最大圆半径
2016-08-24 15:28
253 查看
题目链接
题意:
求凸n边形内最大圆半径解法:
二分圆半径即可,之后对于n边形的每条边,向内推动当前mid距离,进行半平面交,如果有解,证明存在符合条件的半径。代码:
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<vector> #include<iomanip> using namespace std; #define all(x) (x).begin(), (x).end() #define for0(a, n) for (int (a) = 0; (a) < (n); (a)++) #define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++) #define mes(a,x,s) memset(a,x,(s)*sizeof a[0]) #define mem(a,x) memset(a,x,sizeof a) #define ysk(x) (1<<(x)) typedef long long ll; typedef pair<int, int> pii; const int INF =0x3f3f3f3f; const int maxn= 10000 ; const double PI=acos(-1.0); const double eps=1e-10; const double eps2=1e-5; int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } struct Point { double x,y; Point(double x=0,double y=0):x(x),y(y) {}; bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;} bool operator<(const Point& b)const { return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0; } }; typedef Point Vector; Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); } double Cross(Vector A,Vector B)//叉乘 { return A.x*B.y-A.y*B.x; } double Dot(Vector A,Vector B)//点乘 { return 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); } Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); } Vector operator -(Vector A) {return Vector(-A.x,-A.y);} double Length(Vector A) { return sqrt(Dot(A,A)); } struct Line { Point p,p2; Vector v; double ang; Line(){} Line(Point a,Vector v):p(a),v(v){ang=atan2(v.y,v.x); }//点线式 void twoPointIntial(Point p,Point p2)//两点式 { this->p=p; this->p2=p2; v= p2-p; ang=atan2(v.y,v.x); } Point point(double t)//参数方程求参数t对应点 { return p+v*t; } bool operator<(const Line & L)const { return ang<L.ang; } }; Point GetIntersection(Line a,Line b) { Vector u=a.p-b.p; double t=Cross(b.v,u)/Cross(a.v,b.v); return a.p+a.v*t; } bool OnRight(Line L,Point p) { return dcmp(Cross(L.v,p-L.p))<0; } Point p[maxn+10]; Line q[maxn+10]; int HalfplaneIntersection(Line* L,int n) { sort(L,L+n); int first,last; q[first=last=0]=L[0]; for(int i=1;i<n;i++) { while(first<last&&OnRight(L[i],p[last-1] ) ) last--; while(first<last&&OnRight(L[i],p[first])) first++; q[++last]=L[i]; if(fabs(Cross(q[last].v,q[last-1].v) )<eps ) { last--; if(!OnRight(q[last],L[i].p) ) q[last]=L[i]; } if(first<last) p[last-1]=GetIntersection(q[last-1],q[last] ); } while(first<last&&OnRight(q[first],p[last-1])) last--; if(last-first<=1) return 0;//小于3个,不存在 p[last]=GetIntersection(q[last],q[first]); return last-first+1; } Point po[maxn+10]; Line L[maxn+10]; int np; double bs(double le,double ri) { double x[2],y[2]; while(le+eps2<ri) { double mid=(le+ri)/2; for0(i,np) { Vector v=po[(i+1)%np]-po[i]; double len=Length(v); double deltax=mid*v.y/len; double deltay=mid*v.x/len; x[0]=po[i].x- deltax ; y[0]=po[i].y+ deltay ; x[1]=po[(i+1)%np ].x- deltax ; y[1]=po[(i+1)%np ].y+ deltay ; Point S(x[0],y[0]),E(x[1],y[1]); L[i].twoPointIntial(S,E); } if(!HalfplaneIntersection(L,np)) ri=mid; else le=mid; } return ri; } int main() { std::ios::sync_with_stdio(false); while(cin>>np) { for0(i,np) cin>>po[i].x>>po[i].y; cout<<fixed<<setprecision(6)<<bs(0,1e7)<<endl; } return 0; }
相关文章推荐
- 算法笔记4.5.2二分法拓展思考题——求多边形组成的凸边形的外接圆的最大半径
- SGU 332 Largest Circle(凸包内接圆半径nlogn)
- 【无源汇点上下界最大流】[SGU194]Reactor Cooling
- SGU 194 无源汇最大流
- 【codeforces 733D】【贪心 乱搞】D. Kostya the Sculptor【给你n个长方形,让你找出2个或1个长方体,使得他们拼接成的长方体的内接圆半径最大】
- SGU242 最大流 容量限制为2
- whu 1124 最大流和sgu 326 思路一样
- SGU 232 Infinite Fraction(字符串的最大表示)
- sgu194 无源汇上下界的最大流
- SGU194 Reactor Cooling 有下界最大流
- 【无源汇上下界最大流】SGU 194 Reactor Cooling
- [hdu5371 Hotaru's problem]最大回文半径
- sgu 242 Student's Morning--最大流 或 多重匹配
- 一个点的经度和纬度,以这个点为圆心,1000米为半径,最大的经度和纬度,最小的经度和纬度
- sgu 194 上下界最大流 此代码SGU瞬间过,ZOJ瞬间WA
- SGU 194. Reactor Cooling (无源汇有上下界最大流
- Most Distant Point from the Sea - POJ 3525 求多边形最大内接圆半径
- SGU 326 - Perspective(网络流‘最大流)
- SGU 326 Perspective最大流
- sgu-194-Reactor Cooling(无源汇有上下界最大流)