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

来源:互联网 发布:matlab2016a mac 破解 编辑:程序博客网 时间:2024/04/29 13:09

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537


题目大意:给定n个点的坐标,先问这些点是否能组成一个凸包,如果是凸包,问用不相交的线来切这个凸包使得凸包只由三角形组成,根据costi, j = |xi + xj| * |yi + yj| % p算切线的费用,问最少的切割费用。


解题思路:很经典的最优三角剖分模型加一点计算几何的知识。先判断是否为凸包,这个排个序就好弄,我是让队友写个求凸包函数,返回凸包中的顶点数量再与n比较。这一步处理完之后就是用n-3条直线将凸包切成n-2个三角形。

    

     我们要切的是以1和n为起始点的凸包,由于切线不能相交,那么选择1点和n点必有另外一点S要和它们组成一个三角形,然后凸包被分成三个部分:k0,k1,k2,,然后把k1看成一个以n点S点位起始点的凸包,是不是又可以用相同的方法处理这个凸包呢?答案是肯定,就是这样慢慢地将凸包分成一个个子凸包计算费用,最后再更新到点1和点n为起始点的凸包。

    模拟上面的过程,设Dp[i][j]为以i为起点,j为终点的凸包被切割成一个个小三角形所需要的费用。那么dp[i][j] = min(dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]),(j >= i+ 3,i+1<=k<=j-1,cost[i][k]为连一条i到k的线的费用),因为dp[i][j]只表示为以i为起点,j为终点的凸包内部被切割的费用,所以在连线的时候可以加上边界费用而不算重复计算。


测试数据:

3 3
0 0
1 1
0 2

4 100
0 0
1 0
0 1
1 1

4 100
0 0
3 0
0 3
1 1

OutPut:
0
1
I can't cut.


C艹代码:

#include <stdio.h>  #include <string.h>  #include <math.h>  #include <algorithm>  using namespace std;  #define MAX 1000  #define INF 1000000000  #define min(a,b) ((a)<(b)?(a):(b))      struct point{        int x,y;  }p[MAX];  int cost[MAX][MAX],n,m;  int dp[MAX][MAX];      int abs(int x) {        return x < 0 ? -x : x;  }  point save[400],temp[400];  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[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 Count(point a,point b) {        return (abs(a.x + b.x) * abs(a.y+b.y)) % m;  }      int main()  {      int i,j,k,r,u;                  while (scanf("%d%d",&n,&m) != EOF) {                    for (i = 0; i < n; ++i)              scanf("%d%d",&p[i].x,&p[i].y);                      int tot = Graham(p,n);  //求凸包          if (tot < n) printf("I can't cut.\n");          else {                            memset(cost,0,sizeof(cost));              for (i = 0; i < n; ++i)                  for (j = i + 2; j < n; ++j)                      cost[i][j] = cost[j][i] = Count(save[i],save[j]);                                          for (i = 0; i < n; ++i) {                            for (j = 0; j < n; ++j)                      dp[i][j] = INF;                  dp[i][(i+1)%n] = 0;              }              for (i = n - 3; i >= 0; --i) //注意这三个for循环的顺序                  for (j = i + 2; j < n; ++j) //因为要保证在算dp[i][j]时dp[i][k]和dp[k][j]时已经计算,所以i为逆序,j要升序                      for (k = i + 1; k <= j - 1; ++k)                          dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]);                  printf("%d\n",dp[0][n-1]);          }      }  }  


0 0
原创粉丝点击