这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,dp[i][j]表示已经排好序的凸包上的点i->j上被分割成一个个小三角形的最小费用,那么dp[i][j] = min(dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]),其中,(j >= i+ 3,i+1<=k<=j-1,cost[i][k]为连一条i到k的线的费用)。

上一个图,来自博客http://blog.csdn.net/woshi250hua/article/details/7824433

代码如下:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <algorithm>
  7. #define eps 1e-8
  8. using namespace std;
  9. typedef long long ll;
  10. const int maxn = ;
  11. const int inf = ( << );
  12. int dp[maxn][maxn];
  13. int cost[maxn][maxn];
  14. struct point {
  15. int x, y;
  16. };
  17. point p[maxn], convex[maxn];
  18. bool cmp(const point &p1, const point &p2)
  19. {
  20. return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
  21. }
  22. int x_multi(const point &p1, const point &p2, const point &p3)
  23. {
  24. return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
  25. }
  26.  
  27. int sgn(double x)
  28. {
  29. if (fabs(x) < eps)
  30. return ;
  31. return x > ? : -;
  32. }
  33. void convex_hull(point *p, point *convex, int n, int &len)//求凸包
  34. {
  35. sort(p, p + n, cmp);
  36. int top = ;
  37. convex[] = p[];
  38. convex[] = p[];
  39. for (int i = ; i < n; i++)
  40. {
  41. while (top > && x_multi(convex[top - ], convex[top], p[i]) <= )
  42. top--;
  43. convex[++top] = p[i];
  44. }
  45. int tmp = top;
  46. for (int i = n - ; i >= ; i--)
  47. {
  48. while (top > tmp && x_multi(convex[top - ], convex[top], p[i]) <= )
  49. top--;
  50. convex[++top] = p[i];
  51. }
  52. len = top;
  53. }
  54. int get_cost(const point &p1, const point &p2, const int &mod)
  55. {
  56. return (abs(p1.x + p2.x) * abs(p1.y + p2.y)) % mod;
  57. }
  58. int main()
  59. {
  60. int n, mod;
  61. while (~scanf("%d %d", &n, &mod))
  62. {
  63. for (int i = ; i < n; i++)
  64. scanf("%d %d", &p[i].x, &p[i].y);
  65. int len;
  66. convex_hull(p, convex, n, len);
  67. if (len < n)//如果不是凸包的话,
  68. puts("I can't cut.");
  69. else
  70. {
  71. memset(cost, , sizeof(cost));
  72. for (int i = ; i < n; i++)
  73. for (int j = i + ; j < n; j++)
  74. cost[i][j] = cost[j][i] = get_cost(convex[i], convex[j], mod);//计算处各对角的费用
  75. for (int i = ; i < n; i++)//初始化dp
  76. {
  77. for (int j = ; j < n; j++)
  78. dp[i][j] = inf;
  79. dp[i][i + ] = ;
  80. }
  81. for (int i = n - ; i >= ; i--)//必须逆序,因为dp[i][j] 是由dp[i][k], dp[k][j]推来的,而k是大于i的,
  82. for (int j = i + ; j < n; j++)//同理顺序,因为k小于j
  83. for (int k = i + ; k <= j - ; k++)
  84. dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + cost[i][k] + cost[k][j]);
  85. printf("%d\n", dp[][n - ]);
  86. }
  87. }
  88. return ;
  89. }

zoj 3537 Cake(区间dp)的更多相关文章

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

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

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

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

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

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

  4. ZOJ 3537 Cake(凸包判定+区间DP)

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

  5. ZOJ 3537 Cake 求凸包 区间DP

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

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

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

  7. ZOJ 3537 Cake

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

  8. ZOJ 3469Food Delivery(区间DP)

    Food Delivery Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving prob ...

  9. 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 ...

随机推荐

  1. jQuery网页加载进度条插件

    jquery.pace.js会自动监测你的Ajax请求,事件循环滞后,记录您的页面上准备状态和元素来决定的进度情况. 将pace.js和主题css的添加到您的网页! pace.js会自动监测你的Aja ...

  2. VC++ 列表控件的使用方法

    列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值.MFC中使用CListCtrl类来封装列表控件的各种操作. 通过调用BOOL Create( DWORD ...

  3. 解决Maven中Missing artifact javax.jms:jms:jar:1.1:compile

    搭建好项目后报错: Missing artifact javax.jms:jms:jar:1.1:compile  于POM.xml中 解决方案: 一 :在nexus中配置一个代理仓库     地址为 ...

  4. CVE-2015-1635,MS15-034 漏洞测试

    HTTP.sys远程执行代码漏洞(CVE-2015-1635,MS15-034) 远程执行代码漏洞存在于 HTTP 协议堆栈 (HTTP.sys) 中,当 HTTP.sys 未正确分析经特殊设计的 H ...

  5. [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】

    题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...

  6. web 缓存

    http://robbinfan.com/blog/38/orm-cache-sumup http://my.oschina.net/ITBoy/blog/23683 http://www.kuqin ...

  7. nodejs--book

    https://github.com/0xlen/nodejs-wiki-book http://www.nodebeginner.org/index-zh-cn.html http://book.n ...

  8. 跨平台网络通信与服务器框架 acl 3.2.0 发布

    acl 3.2.0 版本发布了,acl 是 one advanced C/C++ library 的简称,主要包括网络通信库以及服务器框架库等功能,支持 Linux/Windows/Solaris/F ...

  9. 具有 Button 风格的 Panel(覆盖TCustomPanel的Paint函数,用到了ThemeServices)

    unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  10. 7.DropDownList的绑定

    ListView中是无法像TextBox等控件那样将DropDownList的选中值绑定到数据字段的,必须编程处理.如例子:人员的性别(男,女,保密),三个值固定写在DropDownList中. 在显 ...