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


题意:

一块多边形状蛋糕,要求全部切成三角形,任意一次cut都要有效将其分成两部分,cut的代价 cost[i][j]=|xi+xj||yi+yj|%p c o s t [ i ] [ j ] = | x i + x j | ∗ | y i + y j | % p ,问最小代价


分析:

由于任意选择两点进行cut都要有效,所以多边形必须为凸多边形。

dp[i][j] d p [ i ] [ j ] 表示 由第 i i 个点至第 j 个点构成的多边形切割的最小代价
我们可以在 (i+1)(j1) ( i + 1 ) → ( j − 1 ) 中选择一点将其分割, (i,k,j) ( i , k , j ) 形成一个三角形,在原基础上 (i,k) ( i , k ) (k,j) ( k , j ) 连边(cut)。则有转移方程

dp[i][j]=min{dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]} d p [ i ] [ j ] = m i n { d p [ i ] [ k ] + d p [ k ] [ j ] + c o s t [ i ] [ k ] + c o s t [ k ] [ j ] }

注意: 凸多边形中,相邻两个点本身就有一条边,不需要切割


代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<time.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>

using namespace std;

#define bll long long

const int maxn = 330;
const int inf = 1e9+7;
struct point
{
    int x,y;
}p[maxn];
int n,mod,cost[maxn][maxn],dp[maxn][maxn];

int det(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}

bool cmp(point a,point b)
{
    return det(a,b,p[0]) > 0;
}

bool graham()
{
    point sta[maxn];
    int u = 0,cnt = 0;
    for (int i=1;i<n;i++)
        if (p[i].y < p[u].y || (p[i].y == p[u].y && p[i].x < p[u].x))
            u = i;
    swap(p[0],p[u]);
    sort(p+1,p+n,cmp);
    p[n] = p[0];

    for (int i=0;i<2;i++) sta[cnt++] = p[i];
    for (int i=2;i<=n;i++)
    {
        if (det(sta[cnt-1],p[i],sta[cnt-2]) > 0)
            sta[cnt++] = p[i];
        else
            return 0;
    }
    return 1;
}

/*----------------------- dp ---------------------------*/
int solve(int i,int j)
{
    if (j <= i+2) return 0;
    if (dp[i][j] != inf) return dp[i][j];
    for (int k=i+1;k<j;k++)
        dp[i][j] = min(dp[i][j],solve(i,k)+solve(k,j)+cost[i][k]+cost[k][j]);
    return dp[i][j];
}

int calc(point a,point b)
{
    return abs(a.x+b.x)*abs(a.y+b.y) % mod;
}

void init()
{
    for (int i=0;i<n;i++) 
        for (int j=i+1;j<n;j++) dp[i][j] = inf;
    for (int i=0;i<n;i++)
        for (int j=i+2;j<n;j++)
            cost[i][j] = cost[j][i] = calc(p[i],p[j]);
    return;
}

int main()
{
    while (scanf("%d %d",&n,&mod)!=EOF)
    {
        for (int i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y);
        if (!graham()) printf("I can't cut.\n");
        else
        {
            init();
            printf("%d\n",solve(0,n-1));
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值