ZOJ 3537 Cake (interval dp, triangular split)

Source: Internet
Author: User

Test instructions

The coordinates of n points on a planar Cartesian coordinate system are given, representing a polygon cake, first, if it is a convex polygon, if no, the output "I can ' t cut." If, then the cake is 3-angle split, cut n-3 times into n-2 triangle cake for small partners to eat, but each cut need a fee, the formula is: cost[i][j] = |xi + xj| * |yi + yj| % p represents the cost of cutting a knife between two points I and J. What is the minimum cost of a question?

Ideas:

To determine whether a convex polygon needs to use the convex hull of the Andrew algorithm, the time complexity of O (Nlogn), and then determine whether the number of points in the convex hull is n on the line. (Great White Book p271)

To find the minimum cost needs to use some of the ideas of divided treatment, of course, mainly DP.

such as the convex polygon (figure from here), if the point 1 and Point n is 1 points to become a triangle, we can enumerate this point K, cut two knives, take out K0 (can not be cut), into K1 and K2 two, to just cut the 1->k and k->n these two edges for the base side, continue to divide and conquer cut down, Until 1 triangles are left. Then the cost of cutting the convex polygon with edge[i][j] as the base edge is Dp[i][j]=max (Dp[i][j], dp[i][k]+dp[i][k]+cost[i][k]+cost[k][j]), The cost of all point pairs can be calculated first. Note that when calculating dp[i][j], dp[i][k] and Dp[k][j] must first be obtained.

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <deque>8#include <algorithm>9#include <vector>Ten#include <iostream> One #definePII pair<int,int> A #defineMax (x, y) (() > (y)? ( x):(y)) - #defineMin (x, y) ((×) < (y)? ( x):(y)) - #defineABS (x) ((x) <0?-(x):(x)) the #defineINF 0x3f3f3f3f - #defineLL Long Long - using namespacestd; - Const DoublePI = ACOs (-1.0); + Const intn= -; -  + structnode A { at     intx, y; - node () {}; -NodeintXinty): x (x), Y (y) {}; - }po[n], path[n]; - intN, p, C[n][n], dp[n][n]; -  in  -InlineintCMP (node A,node b) to { +     if(a.x==b.x)returna.y<b.y; -     returna.x<b.x; the } *InlineintCross (node A,node p1,node p2)//cross Product, A is the new point. If a is to the left of P1->P2, the result is positive.  $ {Panax Notoginseng     return(p1.x-a.x) * (P2.Y-A.Y)-(p2.x-a.x) * (p1.y-a.y); - } the intGet_cost (node A,node b) {returnABS (a.x+b.x) *abs (A.Y+B.Y)%p;}//cost of cutting between A and B +  A intConvexhull (Node *u,intN,node *path)//to find the convex hull, return the number of points in the convex package the { +Sort (u,u+n,cmp);//first press X and then y to sort -     inttop=0; $      for(intI=0; i<n; i++)//Lower Convex package: Left to right $     { -          while(top>1&& Cross (u[i],path[top-1],path[top-2]) <=0) top--;//less than 0, on the right -path[top++]=U[i]; the     } -     intk=top;Wuyi      for(inti=n-2; i>=0; i--)//Upper Convex package: right to left the     { -          while(Top>k && Cross (u[i],path[top-1],path[top-2]) <=0) top--; Wupath[top++]=U[i]; -     } About     if(n>1) top--;//The starting point is repeated, to remove $     returntop; - } -  -  A  + intcal () the { -     if(n==3)return 0;//3 Point 0 charge $Memset (c,0,sizeof(c)); the      for(intI=0; i<n; i++)//the cost of an edge between any two points C the          for(intj=i+2; j<n; J + +) thec[i][j]=c[j][i]=Get_cost (Path[i], path[j]); the      for(intI=0; i<n; i++) -     { in          for(intj=0; j<n; J + +) dp[i][j]=INF; thedp[i][i+1] =0;//Two adjacent points cannot be connected and can be considered as a fee of 0. the     } About      for(intj=2; j<n; J + +)//Ascending the     { the          for(inti=j-2; i>=0; i--)//Descending the         { +              for(intk=i+1; k<j; k++)//Enumerate triangle vertices -Dp[i][j]=min (Dp[i][j], dp[i][k]+dp[k][j]+c[i][k]+c[k][j]); the         }Bayi     } the     returndp[0][n-1]; the } -  - intMain () the { the     //freopen ("Input.txt", "R", stdin); the      while(~SCANF ("%d%d",&n,&p)) the     { -          for(intI=0; i<n; i++) scanf ("%d%d",&po[i].x,&po[i].y); the         if(Convexhull (Po,n,path) <n) puts ("I can ' t cut."); the         Elseprintf"%d\n", Cal ()); the     }94  the     return 0; the}
AC Code

ZOJ 3537 Cake (interval dp, triangular split)

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.