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
Author: LI, Zezhou
Contest: ZOJ Monthly, September 2011
题意:给你n个点,若这n个点组成的多边形是凹多边形时输出I can't cut,否则,将这个凸多边形切成若干个三角形,每切一刀的花费为题上描述的公式,问你切完后的最小花费。
题解:首先判断多边形是否是凸多边形时,跑一发凸包即可,即所有点在凸包上,这里较坑的是,以往求凸包因为求的是最小点,故判定条件有一点细微的区别,最后就是求解最优切法的问题了,我们思考这样一个问题,假如要求从点i到点j所围成的多边形的最优解,在j-i>2的前提下,还剩下最后一刀,呢我们应该怎样让它最优呢?其实只需枚举最后这一刀的位置即可,为什么这样说的,假如最后一刀切在k处,呢(i,k)以及(k,j)一定是最优的,否则就自相矛盾了,故问题已经转化为dp了。。
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
#define inf 1000000000000000
#define MOD 1000000007
#define maxn 1005
#define lowbit(x) (x&-x)
#define eps 1e-9
struct node
{
ll x,y;
}a[305],q[305],st;
ll dp[305][305],cost[305][305];
ll Cross(node p1,node p2,node p3)
{
return (p2.x-p1.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p2.y-p1.y);
}
bool comp(node a,node b)
{
if(Cross(a,b,st)>0)
return 1;
if(Cross(a,b,st)==0 && abs(a.x-st.x)<abs(b.x-st.x))
return 1;
return 0;
}
ll dfs(int x,int y)
{
if(dp[x][y]!=-1)
return dp[x][y];
if(y-x<=2)
return 0;
dp[x][y]=inf;
for(int k=x+1;k<y;k++)
dp[x][y]=min(dp[x][y],dfs(x,k)+dfs(k,y)+cost[x][k]+cost[k][y]);
return dp[x][y];
}
int main(void)
{
ll n,p,i,top,j,k;
while(scanf("%lld%lld",&n,&p)!=EOF)
{
st.x=st.y=1000000;
for(i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
if(a[i].y<st.y || a[i].y==st.y && a[i].x<st.x)
st=a[i];
}
sort(a+1,a+n+1,comp);
q[1]=a[1];q[2]=a[2];top=2;
for(i=3;i<=n;i++)
{
while(top>2 && Cross(q[top-1],a[i],q[top])>0)
top--;
q[++top]=a[i];
}
if(top<n)
{
printf("I can't cut.\n");
continue;
}
memset(dp,-1,sizeof(dp));
memset(cost,0,sizeof(cost));
for(i=1;i<=n;i++)
for(j=i+2;j<=n;j++)
cost[i][j]=cost[j][i]=abs(a[i].x+a[j].x)*abs(a[i].y+a[j].y)%p;
printf("%lld\n",dfs(1,n));
}
return 0;
}