zoj 3537 Cake(区间dp)
这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,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)的更多相关文章
- zoj 3537 Cake 区间DP (好题)
题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...
- 区间DP Zoj 3537 Cake 区间DP 最优三角形剖分
下面是别人的解题报告的链接,讲解很详细,要注意细节的处理...以及为什么可以这样做 http://blog.csdn.net/woshi250hua/article/details/7824433 我 ...
- ZOJ 3537 Cake(凸包+区间DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537 题目大意:给出一些点表示多边形顶点的位置,如果不是凸多边形 ...
- 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 ...
- ZOJ 3537 Cake 求凸包 区间DP
题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价 ...
- zoj 3537 Cake (凸包确定+间隔dp)
Cake Time Limit: 1 Second Memory Limit: 32768 KB You want to hold a party. Here's a polygon-sha ...
- ZOJ 3537 Cake
区间DP. 首先求凸包判断是否为凸多边形. 如果是凸多边形:假设现在要切割连续的一段点,最外面两个一定是要切一刀的,内部怎么切达到最优解就是求子区间最优解,因此可以区间DP. #include< ...
- ZOJ 3469Food Delivery(区间DP)
Food Delivery Time Limit: 2 Seconds Memory Limit: 65536 KB When we are focusing on solving prob ...
- 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 ...
随机推荐
- bom type:Phantom
bom的类型 'type': fields.selection([('normal','Normal BoM'),('phantom','Sets / Phantom')], 'BoM Type', ...
- 【转】C#正则表达式详解
正则表达式通常包含字母文本(Literaltext)和元字符(metacharacter) 字母文本指的是普通文本如"abcde"可匹配字符串中任何包含"abcde&qu ...
- sql新增后返回主键
对于刚学的人来说有点帮助,新增后返回主键有两种方法: 1,返回自增的主键: INSERT INTO 表名 (字段名1,字段名2,字段名3,…) VALUES (值1,值2,值3,…) SELECT @ ...
- [BZOJ 3207] 花神的嘲讽计划Ⅰ【Hash + 可持久化线段树】
题目链接:BZOJ - 3207 题目分析 先使用Hash,把每个长度为 k 的序列转为一个整数,然后题目就转化为了询问某个区间内有没有整数 x . 这一步可以使用可持久化线段树来做,虽然感觉可以有更 ...
- 巧用powerpoint制作符合期刊要求的复合图
- myeclipse spket spket-1.6.23.jar 破解安装教程
一年前安装文档就写过了,今天写破解文档,本来开发js/ext是想用aptana的,但是安装包100多M,我还是用spket吧(才11M),这个需要破解一下license,否则用不了. 一 安装教程如下 ...
- Android Studio使用远程依赖时下载不了jar包的解决方法
使用AS很大的一个好处就是可以使用在线jar包,只需在引用jar包的时候在版本后加上+,比如: compile 'com.facebook.fresco:fresco:0.1.0+' 这样不用在jar ...
- jQuery validate运作流程以及重复提示错误问题
一,运作流程 jQuery validate要想运作,首先要加载相应的js <script type="text/javascript" src="/js/clas ...
- Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
Given inorder and postorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的中序和后续序列,构建出 ...
- C语言变量的理解
1.定义: 变量是一段有名字的连续存储空间.在源代码中通过定义变量来申请并命名这样的存储空间,并通过变量的名字来使用这段存储空间.下面,我们来理解怎样定义一个变量.例如去住酒店.第一步,前台登记:住几 ...