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.
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.
If the cake is not convex polygon-shaped, output “I can’t cut.”. Otherwise, output the minimum cost.
3 3
0 0
1 1
0 2
0
一块多边形状蛋糕,要求全部切成三角形,任意一次cut都要有效将其分成两部分,cut的代价 cost[i][j]=|xi+xj|∗|yi+yj|%p c o s t [ i ] [ j ] = | x i + x j | ∗ | y i + y j | % p ,问最小代价
由于任意选择两点进行cut都要有效,所以多边形必须为凸多边形。
dp[i][j] d p [ i ] [ j ] 表示 由第 i i 个点至第 j j 个点构成的多边形切割的最小代价
我们可以在 (i+1)→(j−1) ( i + 1 ) → ( j − 1 ) 中选择一点将其分割, (i,k,j) ( i , k , j ) 形成一个三角形,在原基础上 (i,k) ( i , k ) (k,j) ( k , j ) 连边(cut)。则有转移方程
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define bll long long
const int maxn = 330;
const int inf = 1e9+7;
struct point
{
int x,y;
}p[maxn];
int n,mod,cost[maxn][maxn],dp[maxn][maxn];
int det(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
bool cmp(point a,point b)
{
return det(a,b,p[0]) > 0;
}
bool graham()
{
point sta[maxn];
int u = 0,cnt = 0;
for (int i=1;iif (p[i].y < p[u].y || (p[i].y == p[u].y && p[i].x < p[u].x))
u = i;
swap(p[0],p[u]);
sort(p+1,p+n,cmp);
p[n] = p[0];
for (int i=0;i<2;i++) sta[cnt++] = p[i];
for (int i=2;i<=n;i++)
{
if (det(sta[cnt-1],p[i],sta[cnt-2]) > 0)
sta[cnt++] = p[i];
else
return 0;
}
return 1;
}
/*----------------------- dp ---------------------------*/
int solve(int i,int j)
{
if (j <= i+2) return 0;
if (dp[i][j] != inf) return dp[i][j];
for (int k=i+1;kreturn dp[i][j];
}
int calc(point a,point b)
{
return abs(a.x+b.x)*abs(a.y+b.y) % mod;
}
void init()
{
for (int i=0;ifor (int j=i+1;jfor (int i=0;ifor (int j=i+2;jreturn;
}
int main()
{
while (scanf("%d %d",&n,&mod)!=EOF)
{
for (int i=0;iscanf("%d %d",&p[i].x,&p[i].y);
if (!graham()) printf("I can't cut.\n");
else
{
init();
printf("%d\n",solve(0,n-1));
}
}
return 0;
}