Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4472
Picture from: http://blog.csdn.net/woshi250hua/article/details/7824433
The minimum cost of splitting a convex hull into several triangles
Solution: The above link has already been detailed, so I will not pay much for it.
# Include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <algorithm> using namespace STD; const int maxn = 310; const double Pi = ACOs (-1.0); struct point {int X, Y; point operator-(const point & T) const {point TMP; TMP. X = x-t. x; TMP. y = Y-t. y; return TMP;} point operator + (const point & T) const {point TMP; TMP. X = x + T. x; TMP. y = Y + T. y; return TMP;} bool operator = = (Const point & T) const {return ABS (x-t.x) = 0 & ABS (y-t.y) = 0 ;}} gp; inline int cross (point, point B, point C) {// return (B. x-a.x) * (C. y-a.y)-(C. x-a.x) * (B. y-a.y );} ********************************* **************************************** * **/bool cmpyx (point, point B) {if (. y! = B. y) return. Y <B. y; return. x <B. x;} void grahamxy (point * P, Int & N) {// horizontal Order (hold: two times space) if (n <3) return; int I, m = 0, Top = 1; sort (p, p + N, cmpyx); for (I = N; I <2 * n-1; I ++) P [I] = P [2 * n-2-i]; for (I = 2; I <2 * n-1; I ++) {While (top> M & cross (P [Top], p [I], p [Top-1]) <0) Top --; P [++ top] = P [I]; if (I = N-1) M = top;} n = top;} int P; int calc (point, point B) {return ABS (. X + B. x) * ABS (. Y + B. y) % P;} Point PT [maxn]; int DP [maxn] [maxn]; int cost [maxn] [maxn]; void min (Int & A, int B) {if (a =-1 | A> B) A = B;} int main () {int N; while (scanf ("% d ", & N, & P )! = EOF) {for (INT I = 0; I <n; I ++) scanf ("% d", & PT [I]. x, & PT [I]. y); int tot = N; grahamxy (PT, TOT); If (TOT <n) {printf ("I can't cut. \ n "); continue;} memset (DP,-1, sizeof (DP); For (INT I = 0; I <n; I ++) for (Int J = I + 2; j <n; j ++) cost [I] [J] = cost [J] [I] = calc (PT [I], PT [J]); For (INT I = 0; I <n; I ++) 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; k ++) {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 ;}