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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define MAXN 100005
#define mod 1000000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
using namespace std; int cmp(int x)
{
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
}
int sqr(int x)
{
return x*x;
}
struct point
{
int x,y;
point(){};
point(int a,int b):x(a),y(b){};
void input()
{
scanf("%d%d",&x,&y);
}
friend point operator +(const point &a,const point &b)
{
return point(a.x+b.x,a.y+b.y);
}
friend point operator -(const point &a,const point &b)
{
return point(a.x-b.x,a.y-b.y);
}
friend bool operator ==(const point &a,const point &b)
{
return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
}
friend point operator *(const point &a,const int &b)
{
return point(a.x*b,a.y*b);
}
friend point operator *(const int &a,const point &b)
{
return point(a*b.x,a*b.y);
}
friend point operator /(const point &a,const int &b)
{
return point(a.x/b,a.y/b);
}
int norm()
{
return sqrt(sqr(x)+sqr(y));
}
};
int det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
}
int dot(const point&a,const point &b)
{
return a.x*b.x+a.y*b.y;
}
int dist(const point &a,const point &b)
{
return (a-b).norm();
} struct polygon_convex
{
vector<point>p;
polygon_convex(int Size=0)
{
p.resize(Size);
}
};
bool comp_less(const point &a,const point &b)
{
return cmp(a.x-b.x)<0||cmp(a.x-b.x)==0&&cmp(a.y-b.y)<0;
}
polygon_convex convex_hull(vector<point> a)
{
polygon_convex res(2*a.size()+5);
sort(a.begin(),a.end(),comp_less);
a.erase(unique(a.begin(),a.end()),a.end());
int m=0;
for(int i=0;i<a.size();i++)
{
while(m>1&&cmp(det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0) m--;
res.p[m++]=a[i];
}
int k=m;
for(int i=int(a.size())-2;i>=0;--i)
{
while(m>k&&cmp(det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0) m--;
res.p[m++]=a[i];
}
res.p.resize(m);
if(a.size()>1) res.p.resize(m-1);
return res;
} int n,m,ans;
int dp[305][305],cost[305][305];
vector<point> pp; int main()
{
int i,j,t;
while(~scanf("%d%d",&n,&m))
{
vector<point> pp;
point tmp;
for(i=1;i<=n;i++)
{
scanf("%d%d",&tmp.x,&tmp.y);
pp.push_back(tmp);
}
polygon_convex tb=convex_hull(pp);
if(tb.p.size()!=n) printf("I can't cut.\n");
else
{
if(n==3)
{
printf("0\n");
continue ;
}
memset(cost,0,sizeof(cost));
for(i=0;i<n;i++)
{
for(j=i+2;j<n;j++)
{
cost[i][j]=(abs(tb.p[i].x+tb.p[j].x)*abs(tb.p[i].y+tb.p[j].y))%m;
}
}
memset(dp,0x3f,sizeof(dp));
for(i=0;i<n-2;i++)
{
dp[i][i+1]=0;
dp[i][i+2]=0;
}
dp[n-2][n-1]=0;
for(int len=4;len<=n;len++)
{
for(i=0;i<n;i++)
{
j=i+len-1;
if(j>=n) break ;
for(int k=i+1;k<=j-1;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]);
}
}
}
printf("%d\n",dp[0][n-1]);
}
}
return 0;
}
/*
3 3
0 0
1 1
0 2 4 10
0 0
2 0
0 2
2 2 5 11
1 1
1 3
3 1
4 2
3 4
*/
版权声明:本文博客原创文章,博客,未经同意,不得转载。
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-shaped c ...
- 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)
这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,dp[i][j]表示已经排好序的凸包上的点i->j上被分割成一个个小三 ...
- 区间DP Zoj 3537 Cake 区间DP 最优三角形剖分
下面是别人的解题报告的链接,讲解很详细,要注意细节的处理...以及为什么可以这样做 http://blog.csdn.net/woshi250hua/article/details/7824433 我 ...
- ZOJ 3537 Cake (区间DP,三角形剖分)
题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2 ...
- ZOJ 3537 (凸包 + 区间DP)(UNFINISHED)
#include "Head.cpp" const int N = 10007; int n, m; struct Point{ int x,y; bool operator &l ...
随机推荐
- TCP/IP详细说明--滑模、拥塞窗口、慢启动、Negle算法
TCP的数据流大致能够分为两类,交互数据流与成块的数据流. 交互数据流就是发送控制命令的数据流.比方relogin,telnet.ftp命令等等.成块数据流是用来发送数据的包,网络上大部分的TCP包都 ...
- mysqldump 命令的使用
1.导出结构不导出数据 mysqldump -d databasename -uroot -p > xxx.sql 2.导出数据不导出结构 mysqldump -t databasename - ...
- Threejs 它可以在建立其内部房间效果可见
Threejs 中建立可看到其内部的房间效果 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...
- Android学习路径(十)如何将Action Bar堆放在布局
默认情况下,action bar出如今activity窗体的顶部,稍微降低了activity布局的总空间.假设你想隐藏或者显示action bar,在这堂用户体验的课程中,你能够通过调用hide() ...
- c++11多线程简介
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...
- json学习初体验--第三者jar包实现bean、List、map创json格式
1.的需要jar包裹json-lib.jar 下载链接: http://sourceforge.net/projects/json-lib/files/json-lib/ 此包还须要下面的依赖包, c ...
- virus.win32.parite.H查杀病毒的方法
virus.win32.parite.H病毒的查杀方法 昨天电脑中了virus.win32.parite.H病毒,搞了2个多小时最终搞定了.以下记录下我的解决方法. 第一步:下载Win32.Parit ...
- node.js基础:数据存储
无服务器的数据存储 内存存储 var http = require('http'); var count = 0; //服务器访问次数存储在内存中 http.createServer(function ...
- HDU 1274 展开字符串 (递归+string类)
题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...
- JSP 获得Spring 注射对象
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%&g ...