【BZOJ1091】【Scoi2003】切割多边形 计算几何 状压DP

计算几何的题一次就能A我真的是感动上苍QAQ

首先由于是凸多边形显然每道边都要切一次,重点是按照什么样的顺序切,注意到一个切了若干刀的图形,如果把切割的顺序换一下,图形不变,并且边很少,不难想到状压DP。

一共256个状态,从低到高转移的时候计算这一次要切多长,我写的是直线带参数的那个式子P=A+t*B,感觉这样算线段长方便一些QAQ

/**************************************************************
    Problem: 1091
    User: RicardoWang
    Language: C++
    Result: Accepted
    Time:8 ms
    Memory:1276 kb
****************************************************************/
 
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define oo 999999999
struct Po
{
    double x,y;
    friend Po operator + (Po x,Po y)
    {
        return (Po){x.x+y.x,x.y+y.y};
    }
    friend Po operator - (Po x,Po y)
    {
        return (Po){x.x-y.x,x.y-y.y};
    }
    friend Po operator * (Po x,double y)
    {
        return (Po){x.x*y,x.y*y};
    }
    friend double operator * (Po x,Po y)
    {
        return x.x*y.x+x.y*y.y;
    }
}A[10],now1,now2,K;
double N,M; int P;
double dp[305];
double Cross(Po x,Po y)
{
    return x.x*y.y-y.x*x.y;
}
void get(double &L,double &R,Po x,Po y)
{
    double op=Cross((y-x),(now2-now1));
    K=(Po){x.y-y.y,y.x-x.x};
    if(op==0)return ;
    else if(op>0)//L
    {
        L=max(L,(K*(x-now1))/(K*(now2-now1)));
    }
    else //R
    {
        R=min(R,(K*(x-now1))/(K*(now2-now1)));
    }
    return ;
}
double getdis(Po x,Po y)
{
    return sqrt((x-y)*(x-y));
}
double calc(int x,int y)
{
    double l=-oo,r=oo;
    now1=A[x]; now2=A[x+1];
    get(l,r,(Po){0,0},(Po){N,0}); //矩形的四个边界
    get(l,r,(Po){N,0},(Po){N,M});
    get(l,r,(Po){N,M},(Po){0,M});
    get(l,r,(Po){0,M},(Po){0,0});
    for(int i=0;i<P;i++)if(y&(1<<i))
    {
        get(l,r,A[i],A[i+1]);
    }
    return (r-l)*(getdis(A[x],A[x+1]));
}
void DP()
{
    memset(dp,0,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<(1<<P);i++)dp[i]=oo;
    for(int i=0;i<(1<<P);i++)
    {
        for(int j=0;j<P;j++)if(!((1<<j)&i))
        {
            dp[i|(1<<j)]=min(dp[i|(1<<j)],dp[i]+calc(j,i));
        }
    }
    printf("%.3lf\n",dp[(1<<P)-1]);
    return ;
}
int main()
{
    //freopen("in.txt","r",stdin);
    cin>>N>>M>>P;
    for(int i=0;i<P;i++)cin>>A[P-1-i].x>>A[P-1-i].y;
    A[P]=A[0];
    DP();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值