Cake

Time Limit: 1 Second Memory Limit: 32768 KB

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 is 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 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, N and p (3 ≤ N, p ≤ 300), indicating the number of vertices. Each line of the following N lines contains two integers, x and y (-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 3

0 0

1 1

0 2

Sample Output

0

首先得判定一下这些点是否可以构成凸包,只要用凸包算法看看这些点构成的凸包的顶点的个数是否等于n。凸包判定直接参考大牛的博客,模板

http://blog.csdn.net/woshi250hua/article/details/7824433

写区间DP的时候注意循环的顺序

关于区间DP,可以参照这个博客

http://blog.csdn.net/dacc123/article/details/50885903

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <stdio.h>
  7. using namespace std;
  8. #define MAX 100000000
  9. int n,p;
  10. struct Node
  11. {
  12. int x,y;
  13. }a[400];
  14. int s[400];
  15. int cos1[400][400];
  16. int dp[400][400];
  17. int top;
  18. int cross(Node a,Node b,Node c)
  19. {
  20. return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
  21. }
  22. int dis(Node a,Node b)
  23. {
  24. return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  25. }
  26. int cmp(Node p1,Node p2)
  27. {
  28. int temp=cross(a[0],p1,p2);
  29. if(temp>0) return true;
  30. else if(temp==0&&dis(a[0],p1)<dis(a[0],p2)) return true;
  31. else return false;
  32. }
  33. int graham(int n)
  34. {
  35. if(n==1){return 0;}
  36. if(n==2){return 1;}
  37. if(n>2)
  38. {
  39. top=1;s[0]=0;s[1]=1;
  40. for(int i=2;i<n;i++)
  41. {
  42. while(top>0&&cross(a[s[top-1]],a[s[top]],a[i])<=0)
  43. top--;
  44. s[++top]=i;
  45. }
  46. return top;
  47. }
  48. }
  49. int cost(Node a,Node b)
  50. {
  51. return abs(a.x+b.x)*abs(a.y+b.y)%p;
  52. }
  53. int main()
  54. {
  55. while(scanf("%d%d",&n,&p)!=EOF)
  56. {
  57. scanf("%d%d",&a[0].x,&a[0].y);
  58. for(int i=1;i<n;i++)
  59. {
  60. scanf("%d%d",&a[i].x,&a[i].y);
  61. if(a[i].y<a[0].y||(a[i].y==a[0].y&&a[i].x<a[0].x))
  62. {
  63. swap(a[i],a[0]);
  64. }
  65. }
  66. sort(a+1,a+n,cmp);
  67. if(graham(n)!=n-1)
  68. {
  69. printf("I can't cut.\n");
  70. continue;
  71. }
  72. for(int i=0;i<n;i++)
  73. {
  74. for(int j=i+2;j<n;j++)
  75. cos1[i][j]=cos1[j][i]=cost(a[i],a[j]);
  76. }
  77. for(int i=0;i<n;i++)
  78. {
  79. for(int j=i;j<n;j++)
  80. dp[i][j]=MAX;
  81. dp[i][(i+1)%n]=0;
  82. }
  83. for(int i=n-3;i>=0;i--)
  84. {
  85. for(int j=i+2;j<n;j++)
  86. {
  87. for(int k=i+1;k<j;k++)
  88. {
  89. dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+cos1[i][k]+cos1[k][j]);
  90. }
  91. }
  92. }
  93. printf("%d\n",dp[0][n-1]);
  94. }
  95. return 0;
  96. }

ZOJ 3537 Cake(凸包判定+区间DP)的更多相关文章

  1. ZOJ - 3537 Cake (凸包+区间DP+最优三角剖分)

    Description You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut t ...

  2. zoj 3537 Cake 区间DP (好题)

    题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...

  3. ZOJ 3537 Cake(凸包+区间DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537 题目大意:给出一些点表示多边形顶点的位置,如果不是凸多边形 ...

  4. ZOJ 3537 Cake 求凸包 区间DP

    题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价 ...

  5. zoj 3537 Cake (凸包确定+间隔dp)

    Cake Time Limit: 1 Second      Memory Limit: 32768 KB You want to hold a party. Here's a polygon-sha ...

  6. ZOJ 3537 Cake

    区间DP. 首先求凸包判断是否为凸多边形. 如果是凸多边形:假设现在要切割连续的一段点,最外面两个一定是要切一刀的,内部怎么切达到最优解就是求子区间最优解,因此可以区间DP. #include< ...

  7. ZOJ 3469 Food Delivery(区间DP)

    https://vjudge.net/problem/ZOJ-3469 题意:在一条直线上有一个餐厅和n个订餐的人,每个人都有随时间上升的不满意值,从餐厅出发,计算出送完时最小的不满意值总和. 思路: ...

  8. zoj 3537 Cake(区间dp)

    这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,dp[i][j]表示已经排好序的凸包上的点i->j上被分割成一个个小三 ...

  9. 区间DP Zoj 3537 Cake 区间DP 最优三角形剖分

    下面是别人的解题报告的链接,讲解很详细,要注意细节的处理...以及为什么可以这样做 http://blog.csdn.net/woshi250hua/article/details/7824433 我 ...

随机推荐

  1. vue2.0动态添加组件

    方法一.<template> <input type="text" v-model='componentName'> <button @click=' ...

  2. 【转】【Linux】linux awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  3. e676. 把彩色图像转换为灰色

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorConvertOp op = new ColorConvertOp(c ...

  4. [mysql] 先按某字段分组再取每组中前N条记录

    From: http://blog.chinaunix.net/uid-26729093-id-4294287.html 请参考:http://bbs.csdn.net/topics/33002126 ...

  5. [转]SQL注入漏洞及绑定变量浅谈

    1.一个问题引发的思考 大家在群里讨论了一个问题,奉文帅之命写篇作文,且看: String user_web = "user_web" String sql = "upd ...

  6. 学习使用资源文件[11] - DLL 中的资源文件

      本例将把一张 bmp 图片, 以资源文件的方式嵌入 dll, 然后再调用. 第一步: 建一个 DLL 工程, 如图: 然后保存, 我这里使用的名称都是默认的. 第二步: 建一个资源原文件, 如图: ...

  7. chrome浏览器开发者工具使用教程[转]

    转自:http://www.cr173.com/html/16930_1.html 更多资源:https://developers.google.com/chrome-developer-tools/ ...

  8. ubuntu压缩

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...

  9. crc16算法,包括单片机和c#版本

    c语言的#include <stdio.h> static  short const wCRC16Table[256] = {         0x0000, 0xC0C1, 0xC181 ...

  10. 转:Android文件操作总结

    http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327597.html http://blog.sina.com.cn/s/blog_5a4 ...