zoj 3537 三角剖分 简单区间DP

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

图片来自 : http://blog.csdn.net/woshi250hua/article/details/7824433

题意:求将一个凸包切成若干个三角形的最小代价


解法:上面的链接已经讲的很详细了,我就不多费口舌了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 310;
const double pi = acos(-1.0);
struct Point {
    int x, y;
    Point operator - (const Point& t) const {
        Point tmp;
        tmp.x = x - t.x;
        tmp.y = y - t.y;
        return tmp;
    }
    Point operator + (const Point& t) const {
        Point tmp;
        tmp.x = x + t.x;
        tmp.y = y + t.y;
        return tmp;
    }
    bool operator == (const Point& t) const {
        return abs(x-t.x) ==0 && abs(y-t.y) ==0;
    }
}GP; 
inline int Cross(Point a, Point b, Point c) {					// 叉积
	return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y); 
}
/***** 凸包 ****************************************************************************/
bool cmpyx(Point a, Point b) {										
	if ( a.y != b.y )
		return a.y < b.y;
	return a.x < b.x;
}
void Grahamxy(Point *p, int &n) {									// 水平序(住:两倍空间)
	if ( n < 3 ) 
		return;
	int i, m=0, top=1;
	sort(p, p+n, cmpyx);	
	for (i=n; i < 2*n-1; i++)
		p[i] = p[2*n-2-i];
	for (i=2; i < 2*n-1; i++) {
		while ( top > m && Cross(p[top], p[i], p[top-1]) < 0 ) 
			top--;
		p[++top] = p[i];
		if ( i == n-1 )	m = top;
	}
	n = top;
}
int p;
int calc(Point a,Point b){
	return abs(a.x+b.x)*abs(a.y+b.y)%p;
}
Point pt[maxn];
int  dp[maxn][maxn];
int cost[maxn][maxn];
void Min(int &a,int b){
	if(a==-1 || a>b) a=b;
}
int main()
{
	int n;
	while(scanf("%d%d",&n,&p)!=EOF)
	{
		for(int i=0;i<n;i++) scanf("%d%d",&pt[i].x,&pt[i].y);
		int tot=n;
		Grahamxy(pt,tot);
		if(tot<n)
		{
			printf("I can't cut.\n");
			continue;
		}
	    memset(dp,-1,sizeof(dp));
		for(int i=0;i<n;i++) 
			for(int j=i+2;j<n;j++) cost[i][j]=cost[j][i]=calc(pt[i],pt[j]);
		for(int i=0;i<n;i++) 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++)
				{
					Min(dp[i][j],dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]);
				}
			}
		}
		printf("%d\n",dp[0][n-1]);
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值