这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,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

代码如下:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define eps 1e-8
using namespace std;
typedef long long ll;
const int maxn = ;
const int inf = ( << );
int dp[maxn][maxn];
int cost[maxn][maxn];
struct point {
int x, y;
};
point p[maxn], convex[maxn];
bool cmp(const point &p1, const point &p2)
{
return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
}
int x_multi(const point &p1, const point &p2, const point &p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
} int sgn(double x)
{
if (fabs(x) < eps)
return ;
return x > ? : -;
}
void convex_hull(point *p, point *convex, int n, int &len)//求凸包
{
sort(p, p + n, cmp);
int top = ;
convex[] = p[];
convex[] = p[];
for (int i = ; i < n; i++)
{
while (top > && x_multi(convex[top - ], convex[top], p[i]) <= )
top--;
convex[++top] = p[i];
}
int tmp = top;
for (int i = n - ; i >= ; i--)
{
while (top > tmp && x_multi(convex[top - ], convex[top], p[i]) <= )
top--;
convex[++top] = p[i];
}
len = top;
}
int get_cost(const point &p1, const point &p2, const int &mod)
{
return (abs(p1.x + p2.x) * abs(p1.y + p2.y)) % mod;
}
int main()
{
int n, mod;
while (~scanf("%d %d", &n, &mod))
{
for (int i = ; i < n; i++)
scanf("%d %d", &p[i].x, &p[i].y);
int len;
convex_hull(p, convex, n, len);
if (len < n)//如果不是凸包的话,
puts("I can't cut.");
else
{
memset(cost, , sizeof(cost));
for (int i = ; i < n; i++)
for (int j = i + ; j < n; j++)
cost[i][j] = cost[j][i] = get_cost(convex[i], convex[j], mod);//计算处各对角的费用
for (int i = ; i < n; i++)//初始化dp
{
for (int j = ; j < n; j++)
dp[i][j] = inf;
dp[i][i + ] = ;
}
for (int i = n - ; i >= ; i--)//必须逆序,因为dp[i][j] 是由dp[i][k], dp[k][j]推来的,而k是大于i的,
for (int j = i + ; j < n; j++)//同理顺序,因为k小于j
for (int k = i + ; k <= j - ; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + cost[i][k] + cost[k][j]);
printf("%d\n", dp[][n - ]);
}
}
return ;
}

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. MAC OSX使用篇

    前述: 从windows平台切换到osx平台,的确没能一下子适应过来,在使用过程当中遇到了很多问题. link1:osx卸载应用程序的四种方法 link2:(推荐看)开始使用Mac OS X——写给M ...

  2. 无线通信技术协议-6LoWPAN

    6LoWPAN:基于IPv6的低速无线个域网标准,即IPv6 over IEEE 802.15.4. 注:6LoWPAN的协议栈实现在Contiki(完整的一套6LoWPAN协议栈).Zephyr(6 ...

  3. awk里的各种坑

    今天又遇到一个,一旦需要定义一个局部数组(awk通过把局部变量定义在函数参数列表来实现局部这一特征)那么这个数组可以正常的操作,但是无法对他取长度,一旦使用length(tempArr)会得到这么一个 ...

  4. ORACLE 日志 logminer 使用

    1. logminer安装 SQL> @$ORACLE_HOME/rdbms/admin/dbmslm.sql Package created. Grant succeeded. Synonym ...

  5. 利用CSP探测网站登陆状态

    0x00 背景 今天看到zone里有同学发帖说了探测支付宝登录状态的帖子:http://zone.wooyun.org/content/17665 由此我想到了我们parsec的@/fd 半年前提到的 ...

  6. ALTER TABLE

    •ALTER TABLE (表名) ADD (列名 数据类型); •ALTER TABLE (表名) MODIFY (列名 数据类型); •ALTER TABLE (表名) RENAME COLUMN ...

  7. RR 插入不影响

    | test100 | CREATE TABLE `test100` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号', `phoneNo` ...

  8. WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞

    漏洞名称: WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞 CNNVD编号: CNNVD-201307-255 发布时间: 2013-07-31 更新时间 ...

  9. BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4 ...

  10. sed删除文本第一个匹配行

    源文本如下,要求删除第一个为happy-123456的行. ----------------------------- aaaaaaa happy- bbbbbb asdasawe happy- ds ...