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
题意:先判断是否为凸包,如果是凸包的话,将其划分成n-2个三角形,使得划分的花费最小。
思路:dp[i][j]表示从i到j的凸包部分都已划分好,dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+solve(i,j)),最后还需要减去一个solve(0,n-1)。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct Point
{
int x,y;
Point(int x=0,int y=0):x(x),y(y){};
};
bool cmp(Point a,Point b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
Point operator - (Point A,Point B){return Point(A.x-B.x,A.y-B.y);}
Point p[310],ch[310];
int Cross(Point A,Point B){return A.x*B.y-A.y*B.x;}
int n,m,MOD,dp[310][310],INF=1e9;
void ConvexHull()
{
sort(p,p+n,cmp);
int i,k;
m=0;
for(i=0;i<n;i++)
{
while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)
m--;
ch[m++]=p[i];
}
k=m;
for(i=n-2;i>=0;i--)
{
while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)
m--;
ch[m++]=p[i];
}
if(n>1)
m--;
}
int solve(int a,int b)
{
return abs(ch[a].x+ch[b].x)*abs(ch[a].y+ch[b].y)%MOD;
}
int main()
{
int i,j,k,d;
while(~scanf("%d%d",&n,&MOD))
{
for(i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
ConvexHull();
if(m!=n)
{
printf("I can't cut.\n");
continue;
}
for(i=0;i<=n-3;i++)
dp[i][i+2]=solve(i,i+2);
for(d=3;d<=n-1;d++)
for(i=0;i+d<=n-1;i++)
{
j=i+d;
dp[i][j]=INF;
for(k=i+1;k<=j-1;k++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
dp[i][j]+=solve(i,j);
}
printf("%d\n",dp[0][n-1]-solve(0,n-1));
}
}