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 我 ...
随机推荐
- 【C++】线程_beginthreadex参数说明
unsigned long _beginthreadex( void * _Security, //第1个参数:安全属性,NULL为默认安全属性 unsigned _StackSize, //第2个参 ...
- Lost connection to MySQL server at 'waiting for initial communication packet', system error: 0
场景: 192.168.7.27 需要访问 192.168.7.175 上的MySQL数据库,连接时报错. 原因: MySQL的配置文件默认没有为远程连接配置好,需要更改下MySQL的配置文件. 解决 ...
- node.js执行shell命令
nodejs功能强大且多样,不只是可以实现 服务器端 与 客户端 的实时通讯,另一个功能是用来执行shell命令 首先,引入子进程模块 var process = require('child_pro ...
- 关于Bundle
1. 黄色的文件夹,打包的时候,不会建立目录,主要保存程序文件 - 素材不允许重名 2. 蓝色的文件夹,打包的时候,会建立目录,可以分目录的存储素材文件 - 素材可以重名 - 游戏的场景,backgr ...
- SharePoint 2013 workflow cannot start automatically when you logged in site as a system account
I have created one simple workflow on custom list using SharePoint designer 2013.While designing wor ...
- bat、cmd、dos窗口:后台调用,不显示黑色的控制台dos(命令行)窗口
建立一个windows的vbs脚本文件,内容类似如下:注意末尾的参数0 createobject("wscript.shell").run "VBoxheadless.e ...
- impala不能查询hive中新增加的表问题
使用Cloudera Manager部署安装的CDH和Impala,Hive中新增加的表,impala中查询不到,其原因是/etc/impala/conf下面没有hadoop和hive相关的 ...
- Windows版Nginx启动失败之1113: No mapping for the Unicode character exists in the target multi-byte code page
Windows版Nginx启动一闪,进程中未发现nginx进程,查看nginx日志,提示错误为1113: No mapping for the Unicode character exists in ...
- 在create-react-app的脚手架里面使用scss
之前用vue-cli脚手架的时候,只需要引进sass需要的依赖包便可以引入scss,但是在create-react-app的时候,发现除了需要引入sass依赖,还需要进行配置: 不管用什么方法进行sa ...
- js中如何删除json对象的某一个选项
我有一个这样一个对象,getData, 但是我不想要每一项的id,那怎么去删除呢(使用delete)? getData.map((item) =>{ delete item["id&q ...