ZOJ 3537 Cake(凸包判定+区间DP)
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
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
#define MAX 100000000
int n,p;
struct Node
{
int x,y;
}a[400];
int s[400];
int cos1[400][400];
int dp[400][400];
int top;
int cross(Node a,Node b,Node c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
int dis(Node a,Node b)
{
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int cmp(Node p1,Node p2)
{
int temp=cross(a[0],p1,p2);
if(temp>0) return true;
else if(temp==0&&dis(a[0],p1)<dis(a[0],p2)) return true;
else return false;
}
int graham(int n)
{
if(n==1){return 0;}
if(n==2){return 1;}
if(n>2)
{
top=1;s[0]=0;s[1]=1;
for(int i=2;i<n;i++)
{
while(top>0&&cross(a[s[top-1]],a[s[top]],a[i])<=0)
top--;
s[++top]=i;
}
return top;
}
}
int cost(Node a,Node b)
{
return abs(a.x+b.x)*abs(a.y+b.y)%p;
}
int main()
{
while(scanf("%d%d",&n,&p)!=EOF)
{
scanf("%d%d",&a[0].x,&a[0].y);
for(int i=1;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].y<a[0].y||(a[i].y==a[0].y&&a[i].x<a[0].x))
{
swap(a[i],a[0]);
}
}
sort(a+1,a+n,cmp);
if(graham(n)!=n-1)
{
printf("I can't cut.\n");
continue;
}
for(int i=0;i<n;i++)
{
for(int j=i+2;j<n;j++)
cos1[i][j]=cos1[j][i]=cost(a[i],a[j]);
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
dp[i][j]=MAX;
dp[i][(i+1)%n]=0;
}
for(int i=n-3;i>=0;i--)
{
for(int j=i+2;j<n;j++)
{
for(int k=i+1;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+cos1[i][k]+cos1[k][j]);
}
}
}
printf("%d\n",dp[0][n-1]);
}
return 0;
}
ZOJ 3537 Cake(凸包判定+区间DP)的更多相关文章
- 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 ...
- zoj 3537 Cake 区间DP (好题)
题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...
- ZOJ 3537 Cake(凸包+区间DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537 题目大意:给出一些点表示多边形顶点的位置,如果不是凸多边形 ...
- 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 3469 Food Delivery(区间DP)
https://vjudge.net/problem/ZOJ-3469 题意:在一条直线上有一个餐厅和n个订餐的人,每个人都有随时间上升的不满意值,从餐厅出发,计算出送完时最小的不满意值总和. 思路: ...
- zoj 3537 Cake(区间dp)
这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,dp[i][j]表示已经排好序的凸包上的点i->j上被分割成一个个小三 ...
- 区间DP Zoj 3537 Cake 区间DP 最优三角形剖分
下面是别人的解题报告的链接,讲解很详细,要注意细节的处理...以及为什么可以这样做 http://blog.csdn.net/woshi250hua/article/details/7824433 我 ...
随机推荐
- Spring Boot 官方文档学习(二)特点
一.SpringApplication banner,就是启动时输出的信息,可以在classpath下添加 banner.txt,或者设置 banner.location 来指向特定的文件.(默认编码 ...
- try catch 异常处理
1.捕获指定异常 2.捕获所有异常(catch(...))
- e681. 基本的打印程序
Note that (0, 0) of the Graphics object is at the top-left of the actual page, outside the printable ...
- (转)MPEG4码流简单分析
把MPEG4码流的分析和它的I,P,B Frame的判定方法在这里简要记录一下吧,供日后的翻看和大家的参考. 测试解码器测试了很久,由于需要将H264和MPEG4的码流进行分析和判断,并逐帧输入解 ...
- java动态代码的实现以及Class的卸载 (转至http://dustin.iteye.com/blog/46393)
JavaWorld一篇题为 Add dynamic code to your application 的文章介绍了如何使用动态代理技术使普通的java源代码具有像jsp一样的动态编译效果,十分有趣. ...
- QMainWindow + QtabWidget 实现 菜单栏 和 标签
from PyQt5.QtWidgets import ( QMainWindow, QMenu, QAction, QTabWidget) if __name__ == '__main__': im ...
- [精]Odoo 8.0深入浅出开发教程-模块开发基础
參考资料点击这里. 构建Odoo模块 模块组成 业务对象 业务对象声明为Python类, 由Odoo自己主动加载. 数据文件 XML或CSV文件格式, 在当中声明了元数据(视图或工作流).配置数据(模 ...
- Digest Authentication 摘要认证
“摘要”式认证( Digest authentication)是一个简单的认证机制,最初是为HTTP协议开发的,因而也常叫做HTTP摘要,在RFC2671中描述.其身份验证机制很简单,它采用杂凑式(h ...
- java获取Mp3播放时长
最近有一个用java获取mp3播放时长的需求,有两种,一种本地文件,一种网络文件,其中获取网络mp3播放时间的方法找了挺久终于找到个能用的了. 第一种很简单,下载个jar包 jaudiotagger ...
- R-CNN目标检测的selective search(SS算法)
候选框确定算法 对于候选框的位置确定问题,简单粗暴的方法就是穷举或者说滑动窗口法,但是这必然是不科学的,因为时间和计算成本太高,直观的优化就是假设同一种物体其在图像邻域内有比较近似的特征(例如颜色.纹 ...