ZOJ 3537 Cake 凸包+区间DP+记忆化搜索

Cake
ZOJ 3537
--------------------------------------------------------------------------------

Time Limit: 1 Second Memory Limit: 32768 KB

--------------------------------------------------------------------------------

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


--------------------------------------------------------------------------------
Author: LI, Zezhou

Contest: ZOJ Monthly, September 2011

题意:给定蛋糕N个顶点的坐标和mod 。

对这N个点,若所构成的图形为凹多边形,则输出"I can't cut." ,。

反之则把该凸多边形切分为多个三角形构成的图形,求所切的最小费用。

所切费用满足 cost[i][j] = cost[j][i] = |xi + xj| * |yi + yj| % p。

首先凸包算法处理。求出顶点数与N比较。处理完后(极角排序)该题即为求最优三角剖分

区间DP ,依然用的是记忆化搜索。

转移方程

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

[ i 。。。 k 。。。 j ]

意为: 从点i到点j所构凸包的最小费用等价划分为从i到k所构凸包的最小费用+从k到j所购凸包的最小费用+ 边(i,k) 和边(k,j)的费用之和。 


代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define min(a,b) (a>b?b:a)
using namespace std;
const int maxn = 300+100 ;
const int Inf = 1<<30 ;
struct Point {
    double x,y,len ;
}Stack[maxn] , Pt[maxn] , Point_A ;

double Cross(Point a, Point b , Point c) {
    return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x) ;
}

double  Dis(Point a , Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x) - (a.y-b.y)*(a.y-b.y)) ;

}

void FindPoint( int n ) {
    int  tempNumber = 0 ;
    Point tempPoint ;
    Point_A = Pt[0] ;
    for( int i = 0 ; i < n ; ++i ) {
        if(Pt[i].y < Point_A.y || Pt[i].y == Point_A.y && Pt[i].x < Point_A.x ) {
            tempNumber = i ;
            Point_A = Pt[i] ;
        }
    }
    tempPoint = Pt[0] ;
    Pt[0] = Pt[tempNumber] ;
    Pt[tempNumber] = tempPoint ;
}

bool Cmp(Point a , Point b){
    double k = Cross(Point_A, a , b ) ;
    if( k > 0 ) return true ;
    if( k < 0 ) return false ;
    a.len = Dis(Point_A, a) ;
    b.len = Dis(Point_A, b) ;
    return a.len < b.len ;
}

int Graham( int n ) {
    int top = 2 ;
    Stack[0] = Pt[0] ;
    Stack[1] = Pt[1] ;
    Stack[2] = Pt[2] ;
    Pt[n] = Pt[0] ;
    for( int i = 3 ; i <= n ; ++i ) {
        while(Cross(Pt[top-1],Pt[top] , Pt[i] )<= 0 && top > 1 ) --top ;
        Stack[++top] = Pt[i] ;
    }
    return top ;
}

int cost[maxn][maxn] ;
int Min[maxn][maxn] ;
void Cost(int n , int mod ) {
    for( int i = 0 ; i < n ; ++i ) {
        for( int j = i+2 ; j < n ; ++j ) {
            cost[i][j] = cost[j][i]  =  (int((abs)(Stack[i].x+Stack[j].x)*(abs)(Stack[i].y+Stack[j].y)))%mod ;
        }
    }
}

int dp(int L , int R ) {
    if( L+2 >= R ) return 0;
    int& ans = Min[L][R] ;
    if( ans > 0 ) return ans ;
    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] ) ;
    }
    return  ans ;
}

int main() {
    int  p ,Num;
    while(~scanf("%d%d",&Num,&p)) {
        for( int i = 0 ; i < Num ; ++i ) {
                scanf("%lf%lf",&Pt[i].x,&Pt[i].y) ;
        }
        FindPoint(Num) ;
        sort(Pt,Pt+Num , Cmp) ;
        int num = Graham(Num) ;
        if( num != Num ) {cout << "I can't cut." << endl ;continue ;}
        memset(Min, 0 , sizeof(Min)) ;
        memset(cost , 0 , sizeof(cost));
        Cost(Num , p) ;
        int c = dp(0,Num-1) ;
        printf("%d\n",c)  ;
    }
    return 0 ;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值