码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-24 23:17:43      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:zoj   names   ems   ber   cpp   log   lock   struct   mem   

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
题意:给定n个点的坐标,先问这些点能否组成一个凸包,假设是凸包,问用不相交的线来切这个凸包使得凸包仅仅由三角形组成。依据costi, j = |xi + xj| * |yi + yj| % p
算切线的费用,问最少的分割费用。

思路:第一次做凸包,抄模板,ZeroClock 图画的非常好,就不反复了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
const int inf = 1000000000;

struct point {
	int x, y;
} p[maxn], save[maxn], tmp[maxn];
int cost[maxn][maxn], n, m;
int dp[maxn][maxn];

int dis(point p1, point p2, point p0) {
	return (p1.x-p0.x) * (p2.y-p0.y) - (p2.x-p0.x) * (p1.y-p0.y);  
}

bool cmp(const point &a, const point &b) {
	if (a.y == b.y) return a.x < b.x;
	return a.y < b.y;
}

int Graham(point *p,int n) {  
	sort(p,p + n,cmp);  
	save[0] = p[0];  
	save[1] = p[1];  
	int top = 1;  
	for (int i = 0;i < n; i++) {  
		while (top && dis(save[top],p[i],save[top-1]) >= 0) top--;  
		save[++top] = p[i];  
	}  

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

int Count(point a, point 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);  

		int tot = Graham(p,n);  //求凸包  
		if (tot != n) printf("I can‘t cut.\n");  
		else {  
			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]);  

			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 - 1; k++)  
						dp[i][j] = 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;
}


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

标签:zoj   names   ems   ber   cpp   log   lock   struct   mem   

原文地址:http://www.cnblogs.com/jzssuanfa/p/6759086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!