Bzoj 1091 ([scoi2003] cutting polygon-cutting line)

Source: Internet
Author: User

1091: [scoi2003] Cut polygon time limit: 1 sec memory limit: 162 MB
Submit: 223 solved: 82
[Submit] [Status] Description

There is a convex P edge (P <= 8), we want to get it through cutting. At the beginning, you have a rectangle of N * m, that is, the coordinates of its four corners are (0, 0), (0, m), (n, 0 ), (n, m ). Each time you select a straight line to cut the current image into two parts, retain the length of one part (the other part is thrown away) of the cutting line. This is the length of the line inside the polygon. Obtain the shortest length of the cutting line. The following is an example. We need to get the middle polygon.

Cut the line 1, 2, 3, 4 to obtain the Quadrilateral in the middle.

Input

The first row has two integers, n, m (0 <n, m <500), and the second row is an integer p (3 <= P <= 8 ). The following P rows have two integers x and y (0 <x <n, 0 <Y <m), which are the coordinates of each vertex given clockwise. The data ensures that the polygon is convex and has no three-point collinearity. The input data is correct.

Output

One row is the total length of the shortest cut line, rounded to the third digit after the decimal point. A 0.001 error is allowed.

Sample input100 100
4
80 80
70 30
20 20
20 80 sample output312.575hint

The sample corresponds to the example given in the figure.

Source



Stretch the polygon directly to a line segment on the rectangle at 2 points. In this way, cut the previous line segment each time you take the polygon.

Ensure vector direction during elongation



#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000007)#define MP make_pair#define MAXP (500+10)#define MAXN (500+10)#define MAXM (500+10)#define eps (1e-6)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}typedef long long ll;int n,m,p;double sqr(double x){return x*x;}int dcmp(double a,double b=0){if (fabs(a-b)<=eps) return 0;else if (a<b) return -1;return 1;}struct P{    double x,y;    P(){}    P(double _x,double _y):x(_x),y(_y){}    friend istream& operator>>(istream& cin,P &a){cin>>a.x>>a.y;return cin;}    friend ostream& operator<<(ostream& cout,P &a){cout<<a.x<<' '<<a.y;return cout;}    friend bool operator==(P a,P b){return dcmp(a.x,b.x)==0&&dcmp(a.y,b.y)==0;    }}a[MAXP];struct V{    double x,y;    V(){}    V(double _x,double _y):x(_x),y(_y){}    V(P a,P b):x(b.x-a.x),y(b.y-a.y){}    friend V operator*(double a,V b){return V(a*b.x,a*b.y);}    friend V operator-(P a,P b){return V(b.x-a.x,b.y-a.y); }    friend double operator*(V a,V b){return a.x*b.y-a.y*b.x;}    friend double operator^(V a,V b){return a.x*b.x+a.y*b.y;}    friend P operator+(P a,V b){return P(a.x+b.x,a.y+b.y);    }    friend double dis2(V a){return sqr(a.x)+sqr(a.y);    }};struct L{    P p;    V v;        L(){}    L(P _A,V _B):p(_A),v(_B){}    friend bool parallel(L a,L b) {return (dcmp(a.v.x*b.v.y,a.v.y*b.v.x))==0;}    friend P intersect(L a,L b) //直线交点    {        V &v=a.v,&w=b.v,u=V(b.p,a.p);        double t=(w*u)/(v*w);        P c=a.p+t*v; return c;    }    friend bool inleft(P a,L b){return dcmp(b.v*V(b.p,a))>=0;    }    void print(){cout<<p.x<<' '<<p.y<<' '<<v.x<<' '<<v.y<<endl;    }}l[MAXP],lrec[4];bool inrec(P a){return (dcmp(a.x)>=0&&dcmp(a.x,n)<=0&&dcmp(a.y)>=0&&dcmp(a.y,m)<=0);}L through_rec_line(L l){    int siz=0;P st[3];    if (dcmp(l.v.x)==0) return L(P(l.p.x,l.v.y>0?0:m),V(0,(l.v.y>0?1:-1)*m));    if (dcmp(l.v.y)==0) return L(P(l.v.x>0?0:n,l.p.y),V((l.v.x>0?1:-1)*n,0)); //至此保证不平行坐标系     Rep(i,4)    {        if (parallel(lrec[i],l)) continue;        st[++siz]=intersect(lrec[i],l);        if (!inrec(st[siz])) siz--;        if (siz==2)        {             if (st[1]==st[2]) siz--;            else            {                V a=V(st[1],st[2]);                if (dcmp(a^l.v)<0) return L(st[2],V(st[2],st[1]));                return L(st[1],a);             }        }    }}bool b[MAXP]={0};double ans=1e300;int cut_list[MAXN];void dfs(double tot,int siz){    if (tot>ans) return;    if (siz==p)     {    //  Rep(i,siz) cout<<cut_list[i]<<' ';printf("%.3lf\n",ans);        ans=min(ans,tot);         return;    }         /*      if (siz==2)    {        if (cut_list[0]==2&&cut_list[1]==1)         {            cout<<' ';        }    }*/       For(i,p)        if (!b[i])        {            L x=through_rec_line(l[i]);            For(j,p)                if (!parallel(l[j],x)&&b[j])                {                    P p=intersect(x,l[j]);                    if (dcmp(V(p,x.p)^V(p,x.p+x.v))<0)                    {                        if (!inleft(x.p,l[j])) x=L(p,V(p,x.p+x.v));                        else if (!inleft(x.p+x.v,l[j])) x=L(x.p,V(x.p,p));                    }                }            b[i]=1;            cut_list[siz]=i;        /*            Rep(j,siz) printf("\t");            cout<<i<<endl;             printf("%.3lf\n",tot+sqrt(dis2(x.v)));          */             dfs(tot+sqrt(dis2(x.v)),siz+1);            b[i]=0;        }}int main(){//  freopen("bzoj1091.in","r",stdin);//  freopen("bzoj1091.out","w",stdout);         cin>>n>>m>>p;    ForD(i,p) cin>>a[i];memcpy(a+p+1,a+1,sizeof(P)*p);    For(i,p) l[i]=L(a[i],V(a[i],a[i+1]));memcpy(l+p+1,l+1,sizeof(L)*p);     lrec[0]=L(P(0,0),V(n,0)),lrec[1]=L(P(n,0),V(0,m)),lrec[2]=L(P(n,m),V(-n,0)),lrec[3]=L(P(0,m),V(0,-m));    For(i,p) l[i]=through_rec_line(l[i]);  //  For(i,p) l[i].print();     dfs(0,0);              printf("%.3lf\n",ans);    return 0;}






Bzoj 1091 ([scoi2003] cutting polygon-cutting line)

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.