Zoj 3537 Cake (convex hull OK + interval DP)

Source: Internet
Author: User

Cake Time limit: 1 Second Memory Limit: 32768 KB

You are want to the A party. Here's a polygon-shaped cake on the table. You're like-to-cut the cake into several triangle-shaped parts for the invited comers. You had a knife to cut. The trace of each cut was a line segment, whose and endpoints is the vertices of the polygon. Within the polygon, any and cuts ought to be disjoint. Of course, the situation that's only the endpoints of the segments intersect are allowed.

The cake ' s considered as a coordinate system. You have known the coordinates of Vexteces. Each cut have a cost related to the coordinate of the vertex, whose formula are 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 is in a line. And the cake is a convex.

Input

There ' re multiple cases. There ' s a blank line between and cases. The first line of each case contains the integers, N and p (3≤ N, p ≤300), indicating the number of vertices. Each line N of the following lines contains-integers, x and y ( -10000≤ x, y ≤10000), indicating the coord Inate of a vertex. You have known that no vertices is 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


Test Instructions:Given the coordinates of N points, first ask whether these points can form a convex hull, assuming convex hull. Ask to use disjoint lines to cut the convex hull so that the convex hull consists only of triangles, according to costi, j = |xi + xj| * |yi + yj| % P calculates the cost of the tangent, asking for a minimum split fee.



Ideas: determine the convex hull first, to find out the number of points after the convex package has changed.

then interval dp,dp[i][j] means cut convex polygon i~ Minimum cost for J, special case the number of vertices is 2 or 3 o'clock do not cut to 0. i~ J introduces two tangent lines ik and kj to divide a convex polygon into two convex polygons and a triangle. transfer Dp[i][j]=min (DP I [K]+dp[k][j]+cost[i][k]+cost[k][j]); cost[i][j] for the cut I, J two points when the cost, when j=i+ 1 o'clock costs 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;i    NT 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&AMP;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))) &L        t;=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*/





Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Zoj 3537 Cake (convex hull OK + interval DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.