Cake(凸包+区间DP)
Posted zdragon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cake(凸包+区间DP)相关的知识,希望对你有一定的参考价值。
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[305][305],sum[305][305]; struct point { int x,y; }p[305]; 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[0],a,b)>0) return 1; return 0; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int pos=0; for(int i=0;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=1; swap(p[pos],p[0]); sort(p+1,p+n,cmp); int cnt=1; for(int i=2;i<n;i++) if(cross(p[i-2],p[i-1],p[i])<0) {flag=0;break;} if(!flag) printf("I can‘t cut.\n"); else { memset(sum,0,sizeof sum); for(int i=0;i<n;i++)///预处理cost for(int j=i+2;j<n;j++) { if(i==0&&j==n-1) 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=0;i<n;i++) { for(int j=0;j<n;j++) dp[i][j]=INF; 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]+sum[i][k]+sum[k][j]); printf("%d\n",dp[0][n-1]); } } return 0; }
以上是关于Cake(凸包+区间DP)的主要内容,如果未能解决你的问题,请参考以下文章
ZOJ 3537 Cake (凸包 + 区间DP && 最优三角形剖分)
[hdu contest 2019-07-29] Azshara's deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法