Zoj 3537 Cake (interval dp)

Source: Internet
Author: User

This topic is the classic convex hull of the optimal triangulation, but this topic may not be convex bag, so to determine in advance whether it is convex, if it is convex bag to continue to split, Dp[i][j] is already in the order of the convex hull on the point i->j is divided into small triangles of the minimum cost, Then dp[i][j] = min (Dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]), wherein, (J >= i+ 3,i+1<=k<=j-1,cost[i][k] is the cost of a line that connects one I to K )。

Previous diagram from the blog http://blog.csdn.net/woshi250hua/article/details/7824433

The code is as follows:

#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#defineEPS 1e-8using namespaceStd;typedefLong Longll;Const intMAXN = -;Const intINF = (1<< -);intDP[MAXN][MAXN];intCOST[MAXN][MAXN];structPoint {intx, y;}; Point P[MAXN], CONVEX[MAXN];BOOLcmpConstPoint &p1,ConstPoint &p2) {    return(P1.y = = P2.y && p1.x < p2.x) | | P1.y <p2.y);}intX_multi (ConstPoint &p1,ConstPoint &AMP;P2,ConstPoint &p3) {    return((p3.x-p1.x) * (P2.Y-P1.Y)-(p2.x-p1.x) * (P3.Y-p1.y));}intSgnDoublex) {    if(Fabs (x) <EPS)return 0; returnX >0?1: -1;}voidConvex_hull (Point *p, point *convex,intNint&len)//Convex bag{sort (p, p+N, CMP); inttop =1; convex[0] = p[0]; convex[1] = p[1];  for(inti =2; I < n; i++)    {         while(Top >0&& X_multi (Convex[top-1], Convex[top], p[i]) <=0) Top--; convex[++top] =P[i]; }    intTMP =top;  for(inti = n-2; I >=0; i--)    {         while(Top > TMP && x_multi (Convex[top-1], Convex[top], p[i]) <=0) Top--; convex[++top] =P[i]; } Len=top;}intGet_cost (ConstPoint &p1,ConstPoint &AMP;P2,Const int&MoD) {    return(ABS (p1.x + p2.x) * ABS (P1.Y + p2.y))%MoD;}intMain () {intN, MoD;  while(~SCANF ("%d%d", &n, &MoD)) {         for(inti =0; I < n; i++) scanf ("%d%d", &p[i].x, &p[i].y); intLen;        Convex_hull (p, Convex, N, Len); if(Len < N)//if it's not a convex bag,Puts"I can ' t cut."); Else{memset (cost,0,sizeof(cost));  for(inti =0; I < n; i++)                 for(intj = i +2; J < N; J + +) Cost[i][j]= Cost[j][i] = Get_cost (Convex[i], convex[j], MoD);//calculate the cost of the opposite corner             for(inti =0; I < n; i++)//Initialize DP            {                 for(intj =0; J < N; J + +) Dp[i][j]=inf; Dp[i][i+1] =0; }              for(inti = n-3; I >=0; i--)//must reverse, because DP[I][J] is pushed by dp[i][k], Dp[k][j], and K is greater than I,                 for(intj = i +2; J < N; J + +)//in the same order, because K is less than J                     for(intK = 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;}
View Code

Zoj 3537 Cake (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.