【kuangbin带你飞-区间DP-1】A-cake-ZOJ3537

题意

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

算法:先算凸包;一定要将点集凸包序列化再计算cost函数,凸包相关注意:点乘判断方向,单调栈思想,比较凸包中点的数量变化

#include
#include
#include
using namespace std;
const int maxn = 500 + 5;
const int inf = (1<<30);
int dp[maxn][maxn];
int cost[maxn][maxn];
struct Points {
    int x, y;
};
Points ps[maxn], convex[maxn];
int crossMul(const Points &p1, const Points &p2, const Points &p3)
{
    return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
bool cmp(const Points &p1, const Points &p2)
{
    return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
}

int getCost(Points& p1, Points& p2,int p) {
    return (abs(p1.x + p2.x) * abs(p1.y + p2.y)) % p;
}
void convex_hull(Points *ps, Points *convex, int n, int &len)//求凸包
{
    sort(ps, ps+ n, cmp);
    int top = 1;
    convex[0] = ps[0];
    convex[1] = ps[1];
    for (int i = 2; i < n; i++)
    {
        while (top > 0 && crossMul(convex[top - 1], convex[top], ps[i]) <= 0)
            top--;
        convex[++top] = ps[i];
    }
    int tmp = top;
    for (int i = n - 2; i >= 0; i--)
    {
        while (top > tmp && crossMul(convex[top - 1], convex[top], ps[i]) <= 0)
            top--;
        convex[++top] = ps[i];
    }
    len = top;
}
void solve() {
    //
    int n,mod;
    while (cin >> n >> mod) {
        for (int i = 0; i < n; i++)
            cin >> ps[i].x >> ps[i].y;
        //memset(dp, inf, sizeof(dp));
        memset(cost, 0, sizeof(cost));
      	int len;
        convex_hull(ps, convex, n, len);
        if (len < n)//如果不是凸包的话,
            puts("I can't cut.");
        else{
						for (int i = 0; i < n; i++) {
              for (int j = i + 2; j < n; j++) {
                  cost[i][j] = cost[j][i] = getCost(con[i], ps[j],mod);
              }
       			 }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    dp[i][j] = inf;
                }
                dp[i][(i + 1)] = 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]);
        }

    }
}
int main() {
    solve();
}

你可能感兴趣的:(算法,动态规划求解)