ZOJ 3537 - Cake (凸包+区间DP)

9 篇文章 0 订阅
2 篇文章 0 订阅
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 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



POINT:

先判断是否是严格凸多边形。然后简单的区间DP


#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;

#define SIZE 333

struct point_t{
	LL x,y;
}P[SIZE];

//叉积,OA×OB
LL cross(point_t const&O,point_t const&A,point_t const&B){
	LL xoa = A.x - O.x;
	LL yoa = A.y - O.y;
	LL xob = B.x - O.x;
	LL yob = B.y - O.y;
	return xoa * yob - xob * yoa;
}

//A如果比B更靠下更靠左返回真
bool isLowLeft(point_t const&A,point_t const&B){
	return A.y < B.y || ( A.y == B.y && A.x < B.x );
}

//按照对于pO的极角排序,极角相等的距离远的排在前面,因为后面要做一个unique
point_t* pO;
bool comp4Graham(point_t const&A,point_t const&B){
	LL t = cross(*pO,A,B);
	if ( t ) return t > 0LL;

	LL a1 = A.x > pO->x ? A.x - pO->x : pO->x - A.x;
	LL a2 = B.x > pO->x ? B.x - pO->x : pO->x - B.x;
	if ( a1 != a2 ) return a1 > a2;

	a1 = A.y > pO->y ? A.y - pO->y : pO->y - A.y;
	a2 = B.y > pO->y ? B.y - pO->y : pO->y - B.y;
	return a1 > a2;
}

//相对于pO是否极角相等
bool isEqPolar(point_t const&A,point_t const&B){
	return 0LL == cross(*pO,A,B);
}

//Graham求凸包,结果当中没有共线点,起点总是最下最左点
int Graham(point_t P[],int n){
	if ( 1 == n ) return 1;

	//寻找最下最左点
	point_t *p = min_element(P,P+n,isLowLeft);

	//交换
	swap(*p,P[0]);

	if ( 2 == n ) return 2;

	//按极角排序,极角相等,距离近的排在前面
	pO = P;
	sort(P+1,P+n,comp4Graham);

	//将相对于pO的共线点均剔除,只保留最后一个
	p = unique(P+1,P+n,isEqPolar);
	n = p - P;

	//真正的Graham循环
	int top = 2;
	for(int i=2;i<n;++i){
		while( top > 1 && cross(P[top-2],P[top-1],P[i]) <= 0LL )
			--top;
		P[top++] = P[i];
	}
	return top;
}

LL dp[333][333];

const LL inf = 0x3f3f3f3f;
int n,p;
LL val(int x,int y)
{
	x--,y--;
	if(abs(x-y)<=1) return 0LL;
	return abs(P[x].x+P[y].x)*abs(P[y].y+P[x].y)%p;
}
int main()
{

	while(~scanf("%d %d",&n,&p)){
		for(int i=0;i<n;++i)scanf("%lld%lld",&P[i].x,&P[i].y);
		int nn = Graham(P,n);
		if(nn!=n){
			printf("I can't cut.\n");
		}else{
			for(int len=4;len<=n;len++){
				for(int i=1;i<=n-len+1;i++){
					int j=i+len-1;
					dp[i][i+2]=0;
					dp[i][j]=inf;
					for(int k=i+1;k<j;k++){
						dp[i][j]=min(dp[i][j],val(i,k)+val(k,j)+dp[i][k]+dp[k][j]);
					}
				}
			}
			printf("%lld\n",dp[1][n]);
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值