Cake
Time Limit: 1000 msMemory 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
之前看题解写的,觉得写的好敷衍,就重新再做。
凸包判断+区间dp
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
int n,p;
struct node
{
int x,y;
}w[310],v[310];
bool cmp(node a,node b)
{
if(a.x==b.x)return a.y<b.y;
else return a.x<b.x;
}
bool check(node a,node b,node c)
{
if((c.y-a.y)*(c.x-b.x)-(c.y-b.y)*(c.x-a.x)>0)return false;
else return true;
}
int C()
{
int cnt=0;
sort(w+1,w+1+n,cmp);
for(int i=1;i<=n;i++)
{
while(cnt>1&&check(v[cnt-2],v[cnt-1],w[i]))cnt--;
v[cnt++]=w[i];
}
int k=cnt;
for(int i=n-1;i>=1;i--)
{
while(cnt>k&&check(v[cnt-2],v[cnt-1],w[i]))cnt--;
v[cnt++]=w[i];
}
if(n>1)cnt--;
return cnt;
}
int cal(node a,node b){return abs(a.x+b.x)*abs(a.y+b.y)%p;}
int dp[310][310],f[310][310];
int main()
{
while(~scanf("%d%d",&n,&p))
{
for(int i=1;i<=n;i++)scanf("%d%d",&w[i].x,&w[i].y);
if(n==3){printf("0\n");continue;}
if(C()<n){printf("I can't cut.\n");continue;}
memset(f,0,sizeof f);
for(int i=1;i<=n;i++)
{
for(int j=i+2;j<=n;j++)
{
f[i][j]=f[j][i]=cal(v[i],v[j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j]=INF;
}
if(i<n)dp[i][i+1]=0;
else dp[i][1]=0;
}
for(int len=2;len<=n;len++)
{
for(int i=1;i+len<=n;i++)
{
int j=i+len;
for(int k=i;k<=j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+f[i][k]+f[k][j]);
}
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}