Cake(凸包+区间DP)

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

题目大意:

给定若干个点,若围成的不是凸包,则输出"I can‘t cut.",反之,把该图形分割成若干个三角,每条分割线不相交,每次分割需要花费

|xi+xj|*|yi+yj|%p,求最小花费.

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[305][305],sum[305][305];
struct point
{
    int x,y;
}p[305];
int cross(point a,point b,point c)///叉积
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool cmp(point a,point b)///极角排序
{
    if(cross(p[0],a,b)>0) return 1;
    return 0;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int pos=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            if(p[pos].y>p[i].y||p[pos].y==p[i].y&&p[pos].x>p[i].x)///找最左下的点
                pos=i;
        }
        int flag=1;
        swap(p[pos],p[0]);
        sort(p+1,p+n,cmp);
        int cnt=1;
        for(int i=2;i<n;i++)
            if(cross(p[i-2],p[i-1],p[i])<0)
                {flag=0;break;}
        if(!flag) printf("I can‘t cut.\n");
        else
        {
            memset(sum,0,sizeof sum);
            for(int i=0;i<n;i++)///预处理cost
                for(int j=i+2;j<n;j++)
                {
                    if(i==0&&j==n-1) continue;
                    sum[i][j]=sum[j][i]=(abs(p[i].x+p[j].x)*abs(p[i].y+p[j].y))%m;
                }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                    dp[i][j]=INF;
                dp[i][(i+1)%n]=0;
            }
            for(int i=n-3;i>=0;i--)
                for(int j=i+2;j<n;j++)
                    for(int k=i+1;k<j;k++)
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+sum[i][k]+sum[k][j]);
            printf("%d\n",dp[0][n-1]);
        }
    }
    return 0;
}
 

原文地址:https://www.cnblogs.com/zdragon1104/p/9142574.html

时间: 2024-08-29 14:28:21

Cake(凸包+区间DP)的相关文章

ZOJ - 3537 Cake (凸包+区间DP+最优三角剖分)

Description 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 endpoin

ZOJ 3537 Cake 求凸包 区间DP

题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价是多少. 思路:首先判断多边形是否是凸多边形,之后就是区间dp了. 求出凸包后,按逆时针来看. 设置dp[i][j]为从顶点i到顶点j所围成凸多边形的最优解. 枚举切点k (i < k < j) dp[i][j] = min(dp[i][k] + dp[k][j] + cost[i][k] + c

ZOJ 3537 Cake(凸包+区间DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537 题目大意:给出一些点表示多边形顶点的位置,如果不是凸多边形(凸包)则不能切,直接输出"I can't cut."切多边形时每次只能在顶点和顶点间切,每切一次的花费为 cost(i, j) = |xi + xj| * |yi + yj| % p.问把多边形切成最多个不相交三角形的最小代价是多少. 解题思路:先求出凸包,接着可以用区间DP解决,设dp

ZOJ 3537 Cake (区间DP,三角形剖分)

题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2份三角形蛋糕给小伙伴吃,但是每切一次需要一个费用,公式是:cost[i][j] = |xi + xj| * |yi + yj| % p 表示在两点i和j之间切一刀的费用.问最少费用是多少? 思路: 判断是否凸多边形需要用到求凸包的Andrew算法,时间复杂度为O(nlogn),然后判断凸包内的点数是否为n就行

[hdu contest 2019-07-29] Azshara&#39;s deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法

今天hdu的比赛的第一题,凸包+区间dp. 给出n个点m个圆,n<400,m<100,要求找出凸包然后给凸包上的点连线,连线的两个点不能(在凸包上)相邻,连线不能与圆相交或相切,连线不能相交但是可以有公共端点. 首先找出凸包,然后把n*n条边和m个圆算点到直线距离验证一下边是否与圆相交存到e[n][n]里. 然后显然是一个dp,但是我开始看错题目了以为不能有公共端点,能有公共端点的情况考虑一下像一个找三角形的过程,就是区间dp. 区间dp有一点妙的地方是最大区间范围是凸包点数而不用+1,因为连

zoj 3537 Cake (凸包判定+区间dp)

Cake Time Limit: 1 Second      Memory 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 eac

Hdu 2513 区间DP

Cake slicing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 149    Accepted Submission(s): 86 Problem Description A rectangular cake with a grid of m*n unit squares on its top needs to be slice

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi