zoj 3537 Cake(区间DP+最优三角形剖分)待续

1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4472

2、题目大意;

给出一个多边形,将这个多边形用不相交的线段分割成一个一个的三角形,如果不能进行分割输出I can't cut.,每条分割线的代价是|xi + xj| * |yi + yj| % p,求将该多边形分割成三角形的最小代价是多少?

首先判断一下该多边形是不是凸多边形,如果是才能进行分割,如果不是直接返回I can't cut

dp[i][j]表示以i-j为基边的凸多边形分割成三角形的最小代价,

dp[i][j]=min(dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j])

思路参考博客http://blog.csdn.net/woshi250hua/article/details/7824433

3、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 305
#define INF 0x7ffffff
    int n,m;
struct node
{
    int x;
    int y;
}p[N];
node save[N];
int dp[N][N];
int cost[N][N];
bool cmp(const node& a,const node &b){
	if(a.y == b.y)return a.x < b.x;
	return a.y < b.y;
}
int xmult(node p1,node p2,node p0){

	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int Graham(node *p,int n) {

	int i;
	sort(p,p + n,cmp);
	save[0] = p[0];
	save[1] = p[1];
	int top = 1;
	for(i = 0;i < n; i++){

		while(top && xmult(save[top],p[i],save[top-1]) >= 0)top--;
		save[++top] = p[i];
	}
	int mid = top;
	for(i = n - 2; i >= 0; i--){

		while(top>mid&&xmult(save[top],p[i],save[top-1])>=0)top--;
		save[++top]=p[i];
	}
	return top;
}
int DP(int l,int r)
{
   // printf("diaoyong %d %d\n",l,r);
    if(dp[l][r])
    return dp[l][r];
    if(r-l<=2)
    return 0;
    int ans=INF;
    for(int i=l+1;i<r;i++)
    {
        ans=min(ans,DP(l,i)+DP(i,r)+cost[l][i]+cost[i][r]);
    }
   // printf("return %d %d %d\n",l,r,ans);
    return dp[l][r]=ans;
}
int Count(node a,node b) {

	return (abs(a.x + b.x) * abs(a.y+b.y)) % m;
}
int main()
{

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        scanf("%d%d",&p[i].x,&p[i].y);
        if(n<=3)
        {
            printf("0\n");
            continue;
        }
        if(Graham(p,n)<n)
        printf("I can't cut.\n");
        else
        {
            memset(dp,0,sizeof(dp));
            memset(cost,0,sizeof(cost));
            for(int i=0;i<n;i++)
            {
                for(int j=i+2;j<n;j++)
                {
                    cost[i][j]=cost[j][i]=Count(save[i],save[j]);
                    //printf("cost[%d][%d]=%d\n",i,j,cost[i][j]);
                }
            }
            printf("%d\n",DP(0,n-1));
        }
    }
    return 0;
}
/*
3 3
 0 0
 1 1
 0 2

4 100
 0 0
 1 0
 0 1
 1 1

4 100
 0 0
 3 0
 0 3
 1 1
 */


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值