zoj 3537 Cake 区间DP (好题)

时间:2023-03-09 09:53:00
zoj 3537 Cake 区间DP (好题)

题意:切一个凸边行,如果不是凸包直接输出。然后输出最小代价的切割费用,把凸包都切割成三角形。

先判断是否是凸包,然后用三角形优化。

dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+w[i][k]+w[j][k]);

w[i][j]代表i到j点的切割费用。

dp[i][j]:表示以i到j点的最小费用。则可把凸边行分成三个部分的费用。两个凸边行(i,k),(k,j)和两条边的费用(i,k),(j,k),k为枚举的三角形顶点。

Zoj 3537 Cake (DP_最优三角形剖分)

 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<algorithm>
#include<vector>
// #include<malloc.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const double pi = acos(-);
// inline int r(){
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
// return x*f;
// }
const int maxn=;
struct Point{
int x, y;
Point(int x=, int y=):x(x),y(y) { }
}p[maxn]; // typedef Point Vector; int w[maxn][maxn],dp[maxn][maxn],n,m; // Vector operator + (const Vector& A, const Vector& B)
// {
// return Vector(A.x+B.x, A.y+B.y);
// }
// Vector operator - (const Point& A, const Point& B)
// {
// return Vector(A.x-B.x, A.y-B.y);
// }
// double Cross(const Vector& A, const Vector& B)
// {
// return A.x*B.y - A.y*B.x;
// } // Vector Rotate(const Vector& A, double rad)
// {
// return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
// } // bool operator < (const Point& p1, const Point& p2)
// {
// return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
// } // bool operator == (const Point& p1, const Point& p2)
// {
// return p1.x == p2.x && p1.y == p2.y;
// }
// // 点集凸包
// // 如果不希望在凸包的边上有输入点,把两个 <= 改成 <
// // 如果不介意点集被修改,可以改成传递引用
// vector<Point> ConvexHull(vector<Point> p)
// {
// // 预处理,删除重复点
// sort(p.begin(), p.end());
// p.erase(unique(p.begin(), p.end()), p.end()); // int n = p.size();
// int m = 0;
// vector<Point> ch(n+1);
// for(int i = 0; i < n; i++)
// {
// while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
// ch[m++] = p[i];
// }
// int k = m;
// for(int i = n-2; i >= 0; i--)
// {
// while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
// ch[m++] = p[i];
// }
// if(n > 1) m--;
// ch.resize(m);
// return ch;
// }
Point save[],temp[];
int xmult(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 *p,int n) {
int i;
sort(p,p + n,cmp);
save[] = p[];
save[] = p[];
int top = ;
for(i = ;i < n; i++){ while(top && xmult(save[top],p[i],save[top-]) >= )top--;
save[++top] = p[i];
}
int mid = top;
for(i = n - ; i >= ; i--){ while(top>mid&&xmult(save[top],p[i],save[top-])>=)top--;
save[++top]=p[i];
}
return top;
} int main(){
// freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
for(int i=;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
clc(dp,);
clc(w,);
int v;
v=Graham(p,n);
// printf("%d\n",(int)v.size());
if(v<n) printf("I can't cut.\n");
else{
for (int i = ; i < n; ++i)
for (int j = i + ; j < n; ++j)
w[i][j] = w[j][i] =(abs(save[i].x + save[j].x) * abs(save[i].y+save[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]+w[i][k]+w[k][j]);
printf("%d\n",dp[][n-]);
}
}
return ;
}