zoj3537(区间dp)

最有三角形划分,凸包不会也抄了别人的凸包代码一直wa不明觉厉,不过dp算是想对了;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
typedef long long lld;
#define oo 0x3f3f3f3f
#define mod 1000000007
#define maxn 300+5
int dp[maxn][maxn];
int cost[maxn][maxn];
struct Node
{
    int x,y;
}a[maxn];
int n,p,c;
Node save[maxn];

int Cost(int i,int j)
{
    return (abs(save[i].x+save[j].x)*abs(save[i].y+save[j].y))%p;
}


//以下是凸包的代码
//-------------------------------------------------------------------------

bool cmp(const Node& t1,const Node& t2)
{
	if(t1.y == t2.y)return t1.x < t2.x;
	return t1.y < t2.y;
}

int xmult(Node p1,Node p2,Node p0)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

int Graham(int n)
{
	int i;
	sort(a+1,a+1+n,cmp);
	save[1] = a[1];
	save[2] = a[2];
	int top = 2;
	for(i = 1;i <= n; i++)
    {
		while(top && xmult(save[top],a[i],save[top-1]) >= 0)top--;
		save[++top] = a[i];
	}


	int mid = top;
	for(i = n - 1; i >= 1; i--)
    {
		while(top>mid&&xmult(save[top],a[i],save[top-1])>=0)top--;
		save[++top]=a[i];
	}
	return top;
}

//-------------------------------------------------------------------------

int main()
{
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d %d",&a[i].x,&a[i].y);
        int temp=Graham(n);
        if(temp<n)
        {
            printf("I can't cut.\n");
            continue;
        }
        memset(cost,0,sizeof cost);
        for(int i=1;i<=n;i++)
            for(int j=i+2;j<=n;j++)
                cost[i][j]=cost[j][i]=Cost(i,j);
        memset(dp,0x3f,sizeof dp);
        for(int i=1;i<=n;i++)
            dp[i][i+1]=0;
        for(int L=3;L<=n;L++)
        {
            for(int i=1;i+L-1<=n;i++)
            {
                int j=i+L-1;
                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[1][n]);
    }
	return 0;
}
/*
4 3
0 0
0 4
4 0
1 1

4 10
0 0
0 4
4 4
4 0
*/


你可能感兴趣的:(dp,ZOJ)