Zoj-3537 cake (convex hull + interval DP + optimal triangle division)

Source: Internet
Author: User

Description

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 output

0 question: Given the coordinates of N points, first ask whether these points can form a convex bag. If it is a convex bag, ask the non-Intersecting Line 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: The first convex bag, template copying, and zeroclock pictures are good and will not be repeated.
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1005; const int INF = 1000000000; struct point {int X, Y;} p [maxn], save [maxn], TMP [maxn]; int cost [maxn] [maxn], n, m; int DP [maxn] [maxn]; int DIS (point P1, point P2, point P0) {return (p1.x-forwarded X) * (p2.y-forwarded y) -(p2.x-Snapshot X) * (p1.y-Snapshot y);} bool CMP (const point & A, const point & B) {if (. y = B. y) return. x <B. x; return. Y <B. y;} int Graham (point * P, int N) {sort (p, p + N, CMP); save [0] = P [0]; save [1] = P [1]; int Top = 1; for (INT I = 0; I <n; I ++) {While (top & DIS (save [Top], p [I], save [Top-1])> = 0) Top --; save [++ top] = P [I];} int mid = top; For (INT I = N-2; I> = 0; I --) {While (top> mid & DIS (save [Top], p [I], save [Top-1])> = 0) Top --; save [++ top] = P [I];} return top ;} Int count (point a, point B) {return (ABS (. X + B. x) * ABS (. Y + B. y) % m ;}int main () {While (scanf ("% d", & N, & M )! = EOF) {for (INT I = 0; I <n; ++ I) scanf ("% d", & P [I]. x, & P [I]. y); int tot = Graham (p, n); // evaluate the convex hull if (Tot! = N) printf ("I can't cut. \ n "); else {memset (cost, 0, sizeof (cost); For (INT I = 0; I <n; ++ I) for (Int J = I + 2; j <n; ++ J) cost [I] [J] = cost [J] [I] = count (save [I], save [J]); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <n; ++ J) DP [I] [J] = inf; DP [I] [(I + 1) % N] = 0;} For (INT I = N-3; i> = 0; I --) for (Int J = I + 2; j <n; j ++) 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 ;}


Zoj-3537 cake (convex hull + interval DP + optimal triangle division)

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.