1、http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4472
2、题目大意;
给出一个多边形,将这个多边形用不相交的线段分割成一个一个的三角形,如果不能进行分割输出I can't cut.,每条分割线的代价是|xi + xj| * |yi + yj| % p,求将该多边形分割成三角形的最小代价是多少?
首先判断一下该多边形是不是凸多边形,如果是才能进行分割,如果不是直接返回I can't cut
dp[i][j]表示以i-j为基边的凸多边形分割成三角形的最小代价,
dp[i][j]=min(dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j])
思路参考博客http://blog.csdn.net/woshi250hua/article/details/7824433
3、AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 305
#define INF 0x7ffffffint n,m;
struct node
{int x;int y;
}p[N];
node save[N];
int dp[N][N];
int cost[N][N];
bool cmp(const node& a,const node &b){if(a.y == b.y)return a.x < b.x;return a.y < b.y;
}
int xmult(node p1,node p2,node p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int Graham(node *p,int n) {int i;sort(p,p + n,cmp);save[0] = p[0];save[1] = p[1];int top = 1;for(i = 0;i < n; i++){while(top && xmult(save[top],p[i],save[top-1]) >= 0)top--;save[++top] = p[i];}int mid = top;for(i = n - 2; i >= 0; i--){while(top>mid&&xmult(save[top],p[i],save[top-1])>=0)top--;save[++top]=p[i];}return top;
}
int DP(int l,int r)
{// printf("diaoyong %d %d\n",l,r);if(dp[l][r])return dp[l][r];if(r-l<=2)return 0;int ans=INF;for(int i=l+1;i<r;i++){ans=min(ans,DP(l,i)+DP(i,r)+cost[l][i]+cost[i][r]);}// printf("return %d %d %d\n",l,r,ans);return dp[l][r]=ans;
}
int Count(node a,node b) {return (abs(a.x + b.x) * abs(a.y+b.y)) % m;
}
int main()
{while(scanf("%d%d",&n,&m)!=EOF){for(int i=0;i<n;i++)scanf("%d%d",&p[i].x,&p[i].y);if(n<=3){printf("0\n");continue;}if(Graham(p,n)<n)printf("I can't cut.\n");else{memset(dp,0,sizeof(dp));memset(cost,0,sizeof(cost));for(int i=0;i<n;i++){for(int j=i+2;j<n;j++){cost[i][j]=cost[j][i]=Count(save[i],save[j]);//printf("cost[%d][%d]=%d\n",i,j,cost[i][j]);}}printf("%d\n",DP(0,n-1));}}return 0;
}
/*
3 30 01 10 24 1000 01 00 11 14 1000 03 00 31 1*/