Cake ZOJ - 3537(凸包判断+区间dp)

该博客主要探讨了一个关于如何有效地将一个多边形蛋糕切割成多个三角形部分的问题。作者通过建立坐标系,利用凸包判断和区间动态规划算法,计算了在满足条件的情况下,切割蛋糕的最小成本。博客中提供了详细的算法实现,并给出了样例输入和输出。
摘要由CSDN通过智能技术生成

Cake
Time Limit: 1000 msMemory Limit: 32768 KB
You want to hold a party. Here’s a polygon-shaped cake on the table. You’d like to cut the cake into several triangle-shaped parts for the invited comers. You have a knife to cut. The trace of each cut is a line segment, whose two endpoints are two vertices of the polygon. Within the polygon, any two cuts ought to be disjoint. Of course, the situation that only the endpoints of two segments intersect is allowed.

The cake’s considered as a coordinate system. You have known the coordinates of vexteces. Each cut has a cost related to the coordinate of the vertex, whose formula is costi, j = |xi + xj| * |yi + yj| % p. You want to calculate the minimum cost.

NOTICE: input assures that NO three adjacent vertices on the polygon-shaped cake are in a line. And the cake is not always a convex.

Input
There’re multiple cases. There’s a blank line between two cases. The first line of each case contains two integers, N and p (3 ≤ N, p ≤ 300), indicating the number of vertices. Each line of the following N lines contains two integers, x and y (-10000 ≤ x, y ≤ 10000), indicating the coordinate of a vertex. You have known that no two vertices are in the same coordinate.

Output
If the cake is not convex polygon-shaped, output “I can’t cut.”. Otherwise, output the minimum cost.

Sample Input
3 3
0 0
1 1
0 2
Sample Output
0

之前看题解写的,觉得写的好敷衍,就重新再做。
凸包判断+区间dp
在这里插入图片描述

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
int n,p;
struct node
{
    int x,y;
}w[310],v[310];
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.y<b.y;
    else return a.x<b.x;
}
bool check(node a,node b,node c)
{
    if((c.y-a.y)*(c.x-b.x)-(c.y-b.y)*(c.x-a.x)>0)return false;
    else return true;
}
int C()
{
    int cnt=0;
    sort(w+1,w+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        while(cnt>1&&check(v[cnt-2],v[cnt-1],w[i]))cnt--;
        v[cnt++]=w[i];
    }
    int k=cnt;
    for(int i=n-1;i>=1;i--)
    {
        while(cnt>k&&check(v[cnt-2],v[cnt-1],w[i]))cnt--;
        v[cnt++]=w[i];
    }
    if(n>1)cnt--;
    return cnt;
}
int cal(node a,node b){return abs(a.x+b.x)*abs(a.y+b.y)%p;}
int dp[310][310],f[310][310];
int main()
{
    while(~scanf("%d%d",&n,&p))
    {
        for(int i=1;i<=n;i++)scanf("%d%d",&w[i].x,&w[i].y);
        if(n==3){printf("0\n");continue;}
        if(C()<n){printf("I can't cut.\n");continue;}
        memset(f,0,sizeof f);
        for(int i=1;i<=n;i++)
        {
            for(int j=i+2;j<=n;j++)
            {
                f[i][j]=f[j][i]=cal(v[i],v[j]);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=INF;
            }
            if(i<n)dp[i][i+1]=0;
            else dp[i][1]=0;
        }
        for(int len=2;len<=n;len++)
        {
            for(int i=1;i+len<=n;i++)
            {
                int j=i+len;
                for(int k=i;k<=j;k++)
                {
                    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+f[i][k]+f[k][j]);
                }
            }
        }
        printf("%d\n",dp[1][n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值