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

题目大意:

给定若干个点,若围成的不是凸包,则输出"I can't cut.",反之,把该图形分割成若干个三角,每条分割线不相交,每次分割需要花费

|xi+xj|*|yi+yj|%p,求最小花费.

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[][],sum[][];
struct point
{
int x,y;
}p[];
int cross(point a,point b,point c)///叉积
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool cmp(point a,point b)///极角排序
{
if(cross(p[],a,b)>) return ;
return ;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int pos=;
for(int i=;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[pos].y>p[i].y||p[pos].y==p[i].y&&p[pos].x>p[i].x)///找最左下的点
pos=i;
}
int flag=;
swap(p[pos],p[]);
sort(p+,p+n,cmp);
int cnt=;
for(int i=;i<n;i++)
if(cross(p[i-],p[i-],p[i])<)
{flag=;break;}
if(!flag) printf("I can't cut.\n");
else
{
memset(sum,,sizeof sum);
for(int i=;i<n;i++)///预处理cost
for(int j=i+;j<n;j++)
{
if(i==&&j==n-) continue;
sum[i][j]=sum[j][i]=(abs(p[i].x+p[j].x)*abs(p[i].y+p[j].y))%m;
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
dp[i][j]=INF;
dp[i][(i+)%n]=;
}
for(int i=n-;i>=;i--)
for(int j=i+;j<n;j++)
for(int k=i+;k<j;k++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+sum[i][k]+sum[k][j]);
printf("%d\n",dp[][n-]);
}
}
return ;
}
 

Cake(凸包+区间DP)的更多相关文章

  1. 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 ...

  2. ZOJ 3537 Cake(凸包+区间DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537 题目大意:给出一些点表示多边形顶点的位置,如果不是凸多边形 ...

  3. ZOJ 3537 Cake 求凸包 区间DP

    题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价 ...

  4. HDU 6603 Azshara's deep sea(凸包+区间DP)

    由于题目要求,首先维护出一个凸包,然后在凸包上寻找点对关系,用rel[i][j]表示i点和j点之间是否可以连线,又由于维护出来的凸包上的点的个数不多,可以直接枚举点对并枚举所有圆,判断两点直线和圆是否 ...

  5. ZOJ 3537 Cake (区间DP,三角形剖分)

    题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2 ...

  6. ZOJ 3537 (凸包 + 区间DP)(UNFINISHED)

    #include "Head.cpp" const int N = 10007; int n, m; struct Point{ int x,y; bool operator &l ...

  7. [hdu contest 2019-07-29] Azshara's deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法

    今天hdu的比赛的第一题,凸包+区间dp. 给出n个点m个圆,n<400,m<100,要求找出凸包然后给凸包上的点连线,连线的两个点不能(在凸包上)相邻,连线不能与圆相交或相切,连线不能相 ...

  8. 区间DP小结

    也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...

  9. 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 ...

随机推荐

  1. Linux--NiaoGe-Service-01

    安装环境介绍 CentOS 6.9_x86_64 我们选择的是基本安装,也即“Basic Server”. 安装完成后重启来到(runlevel 3)纯文本界面. 例题 批量创建账号:假设有5个账号x ...

  2. JDK NIO SelectionKey bug

    此bug项目中使用elasticSearch中出现的,原因是,nio事件选择器,在特性内核下以及jdk6版本中,出现不hold线程,死循环获取事件的bug,导致cup使用率过高: 此bug在官网已被修 ...

  3. iframe 完全跨域自适应高度

    1.跨域访问页面, 需要访问后台的页面,通过后台调转 2.跨域自适应宽高   思路:通过相互嵌套,获取跨域页面的高度,通过src传回到本域,通过parent方法设置主页的iframe的高度 index ...

  4. 编译安装LAMP之php(fpm模块)

    一,准备工作实验平台为CentOS6.6,先下载所需的安装包,我使用的是php-5.4.26.tar.gz,下载地址 http://mirrors.sohu.com/php/ 编译安装的目录:/usr ...

  5. 关于ie的内存泄漏与javascript内存释放

    最近做一个公司的业务系统,公司要求能尽可能的与c/s近似,也就是如c/s一样,点击文本框可以弹出此项目的相关内容,进行选择输入.     我使用了弹出窗口,然后在子窗口双击选中项目,把选中的值返回给父 ...

  6. python笔记_magic变量和函数

    前言 先扯一点背景知识 PEP8(Python Enhancement Proposal)是一份python的编码规范,链接:http://www.python.org/dev/peps/pep-00 ...

  7. bitcoin 源码解析 - 交易 Transaction

    bitcoin 源码解析 - 交易 Transaction(三) - Script 之前的章节已经比较粗略的解释了在Transaction体系当中的整体运作原理.接下来的章节会对这个体系进行分解,比较 ...

  8. 中间件及tomcat的内存溢出调优

    主要是这三个选项的调整需要根据主机的内存配置 以及业务量的使用情况调节 -Xmx4g -Xms4g -Xmn2g xmx 与xms一般设置为一样 xmn大致设置为xmx xms的三分之一   可以使用 ...

  9. 什么是python 中的顶层代码?

    在python语言中我们经常会听到顶层代码的说法,但是什么是顶层代码? 在python中,我们是以缩进来区分代码层次的,所以顶层代码指的是缩进为0个空格的代码. 看如下例子: PP = 3.14 de ...

  10. Python3基础教程(十五)—— PEP8 代码风格指南

    编程语言不是艺术,而是工作或者说是工具,所以整理并遵循一套编码规范是十分必要的. 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 有很 ...