您的位置:首页 > 其它

zoj 3537 Cake (凸包 最优三角剖分)

2017-03-19 18:53 232 查看
题目链接

这道题目是一道最优三角剖分的题目,其实也是一种区间dp里的一种应用,思路也很简单。这道题目还需要先判断是不是凸包,这里其实主要想记录一下凸包模板==

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
#define sqr(a) ((a) * (a))
#define dis(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))

int n,m;
int f[305][305],dp[305][305];

struct Point
{
int x,y;
};
Point s[305],c[305];

int mult(Point p1,Point p2,Point p0)//叉积
{

return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool cmp(const Point& a,const Point &b)
{

if(a.y == b.y)return a.x < b.x;
return a.y < b.y;
}

int Graham(Point *s, int n)//凸包
{
int i;
sort(s, s+n, cmp);
c[0] = s[0];
c[1] = s[1];
int top = 1;
for(i = 0; i < n; i++)
{
while(top && mult(c[top],s[i],c[top-1]) >= 0)top--;
c[++top] = s[i];
}
int mid = top;
for(i = n - 2; i >= 0; i--)
{
while(top>mid && mult(c[top],s[i],c[top-1])>=0)top--;
c[++top]=s[i];
}
return top;
}

int calc(Point a, Point b)//费用
{
return (abs(a.x + b.x) * abs(a.y+b.y)) % m;
}

int main ()
{
while (scanf ("%d%d",&n,&m)==2)
{
for (int i=0; i<n; i++)
scanf ("%d%d",&s[i].x,&s[i].y);
if (Graham(s, n)<n)//求凸包
cout<<"I can't cut."<<endl;
else
{
CL(f);
for (int i=0; i<n; i++)
{
for (int j=i+2; j<n; j++)
f[i][j] = f[j][i] = calc(c[i], c[j]);
}
memset(dp,0,sizeof(dp));

for(int len=3;len<n;len++)
for(int i=0;i+len<n;i++)
{
int j=len+i;
dp[i][j]=INF;
for(int k=i+1;k<j;k++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+f[i][k]+f[k][j]);
}
cout<<dp[0][n-1]<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: