Cake Time Limit: 1 second memory 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 isCOSTI, 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,NAndP(3 ≤N, P≤ 300), indicating the number of vertices. Each line of the followingNLines contains two integers,XAndY(-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 30 01 10 2
Sample output0
Question: Given the coordinates of N points, I first asked if these points can form a convex bag. If they are convex packets, I would like to use non-intersecting lines to cut the convex bag so that the convex bag is only composed of triangles, based on COSTI, j = | Xi + XJ | * | yi + YJ | % P calculates the tangent cost and the lowest cut cost.
Idea: Determine the convex hull first, and check whether the number of aspect changes after the convex hull. Then the range DP and DP [I] [J] indicate the convex polygon I ~ The minimum cost of J. In special cases, when the number of vertices is 2 or 3, the value 0. I ~ does not need to be switched ~ J introduces two tangent ik and kJ to divide a Convex Polygon into two Convex Polygon and a triangle. Transfer DP [I] [J] = min (DP [I] [k] + dp [k] [J] + cost [I] [k] + cost [k] [j]); cost [I] [J] is the cost when I and j are switched. When J = I + 1, the cost is 0. (convex polygon is a triangle)
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 105#define MAXN 100005#define mod 1000000000#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-8typedef long long ll;using namespace std;int cmp(int x){ if(fabs(x)<eps) return 0; if(x>0) return 1; return -1;}int sqr(int x){ return x*x;}struct point{ int x,y; point(){}; point(int a,int b):x(a),y(b){}; void input() { scanf("%d%d",&x,&y); } friend point operator +(const point &a,const point &b) { return point(a.x+b.x,a.y+b.y); } friend point operator -(const point &a,const point &b) { return point(a.x-b.x,a.y-b.y); } friend bool operator ==(const point &a,const point &b) { return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0; } friend point operator *(const point &a,const int &b) { return point(a.x*b,a.y*b); } friend point operator *(const int &a,const point &b) { return point(a*b.x,a*b.y); } friend point operator /(const point &a,const int &b) { return point(a.x/b,a.y/b); } int norm() { return sqrt(sqr(x)+sqr(y)); }};int det(const point &a,const point &b){ return a.x*b.y-a.y*b.x;}int dot(const point&a,const point &b){ return a.x*b.x+a.y*b.y;}int dist(const point &a,const point &b){ return (a-b).norm();}struct polygon_convex{ vector<point>p; polygon_convex(int Size=0) { p.resize(Size); }};bool comp_less(const point &a,const point &b){ return cmp(a.x-b.x)<0||cmp(a.x-b.x)==0&&cmp(a.y-b.y)<0;}polygon_convex convex_hull(vector<point> a){ polygon_convex res(2*a.size()+5); sort(a.begin(),a.end(),comp_less); a.erase(unique(a.begin(),a.end()),a.end()); int m=0; for(int i=0;i<a.size();i++) { while(m>1&&cmp(det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0) m--; res.p[m++]=a[i]; } int k=m; for(int i=int(a.size())-2;i>=0;--i) { while(m>k&&cmp(det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0) m--; res.p[m++]=a[i]; } res.p.resize(m); if(a.size()>1) res.p.resize(m-1); return res;}int n,m,ans;int dp[305][305],cost[305][305];vector<point> pp;int main(){ int i,j,t; while(~scanf("%d%d",&n,&m)) { vector<point> pp; point tmp; for(i=1;i<=n;i++) { scanf("%d%d",&tmp.x,&tmp.y); pp.push_back(tmp); } polygon_convex tb=convex_hull(pp); if(tb.p.size()!=n) printf("I can't cut.\n"); else { if(n==3) { printf("0\n"); continue ; } memset(cost,0,sizeof(cost)); for(i=0;i<n;i++) { for(j=i+2;j<n;j++) { cost[i][j]=(abs(tb.p[i].x+tb.p[j].x)*abs(tb.p[i].y+tb.p[j].y))%m; } } memset(dp,0x3f,sizeof(dp)); for(i=0;i<n-2;i++) { dp[i][i+1]=0; dp[i][i+2]=0; } dp[n-2][n-1]=0; for(int len=4;len<=n;len++) { for(i=0;i<n;i++) { j=i+len-1; if(j>=n) break ; 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[0][n-1]); } } return 0;}/*3 30 01 10 24 100 02 00 22 25 111 11 33 14 23 4*/