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

来源:互联网 发布:石家庄鸿搜网络怎么样 编辑:程序博客网 时间:2024/04/20 03:55

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 30 01 10 2

Sample Output

0


漏掉了计算公式里的的绝对值。o(╯□╰)o

题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价。现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价是多少。


思路:首先判断多边形是否是凸多边形,之后就是区间dp了。

求出凸包后,按逆时针来看。

设置dp[i][j]为从顶点i到顶点j所围成凸多边形的最优解。

枚举切点k (i < k < j)

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


AC代码:


#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (300+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;struct Point{    int x, y;    Point(){}    Point(int X, int Y){        x = X; y = Y;    }};Point P[MAXN];int dcmp(double x){    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}Point operator + (Point A, Point B){    return Point(A.x+B.x, A.y+B.y);}Point operator - (Point A, Point B){    return Point(A.x-B.x, A.y-B.y);}Point operator * (Point A, int p){    return Point(A.x*p, A.y*p);}int Cross(Point A, Point B){    return A.x*B.y - A.y*B.x;}double Dis(Point A, Point B){    return (double)sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));}bool cmp(Point A, Point B){    int temp = Cross(A-P[0], B-P[0]);    if(temp > 0) return true;    if(temp == 0 && dcmp(Dis(A, P[0]) - Dis(B, P[0])) < 0) return true;    return false;}int Stack[MAXN], top;void input(int n){    scanf("%d%d", &P[0].x, &P[0].y);    Point T = P[0];    int id = 0;    for(int i = 1; i < n; i++)    {        Ri(P[i].x); Ri(P[i].y);        if(P[i].y < T.y || (P[i].y == T.y && P[i].x < T.x))        {            T = P[i];            id = i;        }    }    T = P[0]; P[0] = P[id]; P[id] = T;    sort(P+1, P+n, cmp);}void Graham(int n){    if(n == 1) {top = 0, Stack[0] = 0;}    else if(n == 2)    {        top = 1;        Stack[0] = 0;        Stack[1] = 1;    }    else    {        for(int i = 0; i <= 1; i++)            Stack[i] = i;        top = 1;        for(int i = 2; i < n; i++)        {            while(top > 0 && Cross(P[Stack[top]] - P[Stack[top-1]], P[i]-P[Stack[top-1]]) <= 0) top--;            top++;            Stack[top] = i;        }    }}int ans[MAXN][MAXN];int cost[MAXN][MAXN];int dp(int i, int j){    if(ans[i][j] != -1) return ans[i][j];    if(j - i <= 2)        return 0;    ans[i][j] = INF;    for(int k = i+1; k < j; k++)        ans[i][j] = min(ans[i][j], dp(i, k) + dp(k, j) + cost[i][k] + cost[k][j]);    return ans[i][j];}int main(){    int n, p;    while(scanf("%d%d", &n, &p) != EOF)    {        input(n); Graham(n);        if(top < n-1)        {            printf("I can't cut.\n");            continue;        }        CLR(ans, -1); CLR(cost, 0);        for(int i = 0; i < n; i++)            for(int j = i+2; j < n; j++)                cost[i][j] = cost[j][i] = abs(P[i].x+P[j].x) * abs(P[i].y+P[j].y) % p;        Pi(dp(0, n-1));    }    return 0;}


0 0