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

    1.概述 复制集是一个带有故障转移的主从集群.是从现有的主从模式演变而来,增加了自动故障转移和节点成员自动恢复. 复制集模式中没有固定的主结点,在启动后,多个服务节点间将自动选举 产生一个主结点.该主 ...

  2. 总结几种C#窗体间通讯的处理方法

    摘要:本文介绍了C#窗体间通讯的几种处理方法,即传值.继承.事件回调,希望对大家有用. http://www.cnblogs.com/jara/p/3439603.html 应用程序开发中,经常需要多 ...

  3. 高级停靠(Dock)技术的实现

    高级停靠(Dock)技术的实现 介绍 所谓停靠就是可以用鼠标拖动窗体或者控件,并将其从一个父窗体移出或者移动到另一个父窗体上,可以按水平,垂直方向整齐排列, 并且可以停靠在分页控制组件上.下面的示意图 ...

  4. 一次性安装src.rpm编译所依赖的软件包

    yum-builddep SRPMS/fcitx-4.2.8.4-4.1.cgdl21.src.rpm NAME       yum-builddep - install missing depend ...

  5. 在MAC下 Python+Django+mysql配置

    今天在搭建Django+mysql环境的时候遇到了一点问题,记录下来. 安装环境:OS X 10.10操作系统,Python 2.7. MySQLdb其实包含在MySQL-python包中,因此无论下 ...

  6. 串行CPU设计

    一.概述 串行CPU工作流程 串行CPU的时序流程如下图所示:取指.译码.执行.回写. 其中,取指.回写是与存储器打交道:而译码与执行则是CPU内部自个儿的操作. 我们究竟想要CPU干什么?     ...

  7. C# 正则表达式、Json

    正则表达式: 正则表达式主要的参考文章:http://www.cnblogs.com/stg609/archive/2009/06/03/1492709.html#anchorD. 需求:将cocos ...

  8. Ruby自学笔记(六)— 循环

    循环结构在编程语言中是不可或缺的,所以Ruby中的循环也有其自定义的规则. 而我们关注循环结构,要知道两个因素:1) 循环的条件:2) 循环执行的内容 Ruby有一些方式来实现循环结构体: 1. ti ...

  9. Yaroslav and Sequence

    Codeforces Round #182 (Div. 1) A:http://codeforces.com/contest/301/problem/A 题意:给你2*n-1个数,你每次可以选择n个连 ...

  10. keil MDK启动文件分析---基于LPC2100系列(其实都是相通的)

    转用MDK有一段时间了,越来越觉得MDK的强大,因为我之前都是用ADS1.2开发产品,所以更能体会到MDK的强大与易用性.MDK编译出来的代码与ADS1.2相比,代码量减少了很多,我的一个工程用ADS ...