1. 程式人生 > >zoj 3537 Cake 【凸包 + 區間dp】 【最優三角剖分】

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;
}