# zoj 3537 cake 切蛋糕 区间DP+凸包+递归 最优三角形剖分

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        zoj3537.cpp
*  Create Date: 2013-12-04 16:35:27
*  Descripton:  convex hull + intervel dp
*/

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

#define sqr(a) ((a) * (a))
#define dis(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))

const int MAXN = 305;
const int INF = 0x3c3c3c3c;
const double PI = acos(-1.0);

struct Point {
double x;
double y;
Point(double a = 0, double b = 0) : x(a), y(b) {}
friend bool operator < (const Point &l, const Point &r) {
return l.y < r.y || (l.y == r.y && l.x < r.x);
}
} p[MAXN], ch[MAXN * 2], tmp[MAXN];
// p, point   ch, convex hull

int f[MAXN][MAXN], c[MAXN][MAXN];		// rec and cast
int P;

double mult(const Point &a, const Point &b, const Point &o) {
return (a.x - o.x) * (b.y - o.y) >= (b.x - o.x) * (a.y - o.y);
}

double Graham(Point p[], int n, Point res[]) {
int top = 1;
sort(p, p + n);
if (n == 0) return 0;
res[0] = p[0];
if (n == 1) return 0;
res[1] = p[1];
if (n == 2) return dis(p[0], p[1]) * 2;
res[2] = p[2];
for (int i = 2; i < n; i++) {
while (top && (mult(p[i], res[top], res[top - 1])))
top--;
res[++top] = p[i];
}
int len = top;
res[++top] = p[n - 2];
for (int i = n - 3; i >= 0; i--) {
while (top != len && (mult(p[i], res[top], res[top - 1])))
top--;
res[++top] = p[i];
}
}

int calc(int i, int j) {
return (abs((int)ch[i].x + (int)ch[j].x) * abs((int)ch[i].y + (int)ch[j].y)) % P;
}

int dp(int l, int r) {
if (f[l][r]) return f[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) + c[l][i] + c[i][r]);
}
return f[l][r] = ans;
}

int main() {
int n;
while (~scanf("%d%d", &n, &P)) {
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
if (n <= 3) {
puts("0");
continue;
}
if (Graham(p, n, ch) < n)
puts("I can't cut.");
else {
memset(f, 0, sizeof(f));
for (int i = 0; i < n; i++)
for (int j = i + 2; j < n; j++)
c[i][j] = c[j][i] = calc(i, j);

printf("%d\n", dp(0, n - 1));
}
}
return 0;
}


#### zoj3537cake【最优三角剖分+凸包】

2016-02-19 10:26:30

#### zoj3537 Cake 【凸包+最优三角形划分】

2016-04-06 11:29:31

#### zoj 3537 Cake 【凸包 + 区间dp】 【最优三角剖分】

2015-12-23 12:58:52

#### ACM中基础的切蛋糕问题

2017-04-02 00:22:26

#### uva 1629切蛋糕（dp）

2015-06-30 13:16:26

#### HD ACM 切蛋糕

2013-06-09 20:42:38

#### 切蛋糕

2015-11-16 11:31:20

#### 九度OJ 1551 切蛋糕 计算几何+二分

2014-03-10 12:39:36

#### vijos1020—切蛋糕

2016-05-03 21:28:40

#### 分蛋糕问题 —— 9 个烧饼分给 10 个人

2016-08-19 13:09:23