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 我 ...
随机推荐
- MVC Razor与javascript混编(js中嵌入razor)
其中的关键是输出js上的纯文本内容,让浏览器解析为其中的js代码 <script> BUI.use('common/main',function(){ var conf ...
- 详解JQuery Ajax 在asp.net中使用总结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- Java精选笔记_Java编程基础
Java的基本语法 Java代码的基本格式 修饰符 class 类名 { 程序代码 } 一个Java源文件只定义一个类,不同的类使用不同的源文件定义:将每个源文件中单独定义的类都定义成public ...
- cocos2dx-3.x物理引擎Box2D介绍
理引擎 Cocos2d-x引擎内置了两种物理引擎,它们分别是Box2D和Chipmunk,都是非常优秀的2D物理引擎,而且x引擎将它们都内置在SDK中.Box2D使用较为广泛,在这里选择Box2D来进 ...
- Mybatis头文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- django 文档
django 学习文档 https://yiyibooks.cn/xx/django_182/index.html
- Delphi Code Editor 之 几个特性(转)
Delphi Code Editor有几个特性在编写大规模代码时非常有用.下面分别进行介绍: 原地址:http://www.cnblogs.com/pchmonster/category/343330 ...
- java基础---->FilenameFilter之文件过滤
FilenameFilter用于对列表中文件名的过滤,今天我们就开始java中FilenameFilter的学习.好多年了,你一直在我的伤口中幽居,我放下过天地,却从未放下过你,我生命中的千山万水,任 ...
- Ubuntu 14.04.02 安装openvswitch-2.3.1
Open vSwitch安装 安装好操作系统 # lsb_release -a LSB Version: core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:c ...
- Android 长截屏原理
https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/ a ...