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

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


漏掉了计算公式里的的绝对值。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|1
using 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;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值