HDU 3644 Simulated Annealing

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3644

Question: a polygon with N points, the radius of a circle, determines whether the circle can be placed in a polygon,

Because the circular coordinates are not determined, the simulated annealing method is used to calculate and continuously reduce the step size. N points are selected and points are determined by threading In the polygon,

Precision is very pitfall, tune an afternoon precision, between Wa and TLE lingers 20 + times, vomit blood AC.

Code:

/* ***********************************************Author :rabbitCreated Time :2014/7/3 22:46:38File Name :2.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-4#define pi acos(-1.0)typedef long long ll;int dcmp(double x){    if(fabs(x)<eps)return 0;    return x>0?1:-1;}struct Point{    double x,y;    Point(double _x=0,double _y=0){        x=_x;y=_y;    }};Point operator + (Point a,Point b){    return Point(a.x+b.x,a.y+b.y);}Point operator - (Point a, Point b){    return Point(a.x-b.x,a.y-b.y);}Point operator * (Point a,double p){    return  Point(a.x*p,a.y*p);}Point operator / (Point a,double p){    return Point(a.x/p,a.y/p);}bool operator < (const Point &a,const Point &b){    return a.x<b.x||(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;}double Dot(Point a, Point b){    return a.x*b.x+a.y*b.y;}double Length(Point a){    return sqrt(Dot(a,a));}double Angle(Point a,Point b){    return acos(Dot(a,b)/Length(a)/Length(b));}double angle(Point a){    return atan2(a.y,a.x);}double Cross(Point a,Point b){    return a.x*b.y-a.y*b.x;}Point vecnit(Point x){    return x/Length(x);}Point normal(Point x){    return Point(-x.y,x.x)/Length(x);}Point Rotate(Point a,double rad){    return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}Point GetLineIntersection(Point p,Point v,Point q,Point w){    Point u=p-q;    double t=Cross(w,u)/Cross(v,w);    return p+v*t;}struct Line{    Point p,v;    double ang;    Line(){};    Line(Point _p,Point _v):p(_p),v(_v){        ang=atan2(v.y,v.x);    }    Point point(double a){        return p+(v*a);    }    bool operator < (const Line &L)const{        return ang<L.ang;    }};Point GetLineIntersection(Line a,Line b){    return GetLineIntersection(a.p,a.v,b.p,b.v);}bool OnLeft(const Line &L,const Point &p){    return Cross(L.v,p-L.p)>=0;}bool getdir(Point *p,int n){    double ans=0;    for(int i=0;i<n;i++)        ans+=Cross(p[i],p[(i+1)%n]);    if(dcmp(ans)>0)return 1;    return 0;}bool OnSegment(Point p,Point a1,Point a2){    return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<=0;}double DistanceToSegment(Point p,Point a,Point b){    if(a==b)return Length(p-a);    Point v1=b-a,v2=p-a,v3=p-b;    if(dcmp(Dot(v1,v2))<0)return Length(v2);    else if(dcmp(Dot(v1,v3))>0)return Length(v3);    else return fabs(Cross(v1,v2))/Length(v1);}int isPointInPolygon(Point p,Point *poly,int n){    int wn=0;    for(int i=0;i<n;i++){        if(OnSegment(p,poly[i],poly[(i+1)%n]))return -1;        int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));        int d1=dcmp(poly[i].y-p.y);        int d2=dcmp(poly[(i+1)%n].y-p.y);        if(k>0&&d1<=0&&d2>0)wn++;        if(k<0&&d2<=0&&d1>0)wn--;    }    if(wn!=0)return 1;    return 0;}Point p[60],ret[60];double ans[60];int n;double cal(Point tt){    double ret=INF;    for(int i=0;i<n;i++)        ret=min(ret,DistanceToSegment(tt,p[i],p[(i+1)%n]));    return ret;}int main(){     srand(time(NULL));     while(~scanf("%d",&n)&&n){        double maxx=-INF,minx=INF,maxy=-INF,miny=INF;        for(int i=0;i<n;i++){            scanf("%lf%lf",&p[i].x,&p[i].y);            maxx=max(maxx,p[i].x);            minx=min(minx,p[i].x);            maxy=max(maxy,p[i].y);            miny=min(miny,p[i].y);        }        double R;        scanf("%lf",&R);        bool flag=0;        if(getdir(p,n)==0)reverse(p,p+n);        maxx-=minx;maxy-=miny;        double pp=sqrt(maxx*maxx+maxy*maxy)/2;        p[n]=p[0];        for(int i=0;i<n;i++)            ret[i]=(p[i]+p[i+1])/2;        memset(ans,0,sizeof(ans));        while(!flag&&pp>1e-4){            for(int i=0;!flag&&i<20;i++)                for(int j=0;j<5&&!flag;j++){                    double gg=rand();                    Point temp;                    temp.x=ret[i].x+pp*cos(gg);                    temp.y=ret[i].y+pp*sin(gg);                    if(isPointInPolygon(temp,p,n)){                        double ss=cal(temp);                        if(ss>ans[i]){                            ans[i]=ss;                            ret[i]=temp;                            if(dcmp(ans[i]-R)>=0)flag=1;                        }                    }                }            pp*=0.8;        }            if(flag)puts("Yes");        else puts("No");    }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.