ZOJ - 3537 Cake 凸包+区间DP

本文探讨了一个关于如何将一个多边形蛋糕通过最优路径切割成多个三角形的问题,旨在找到最低成本的切割方案。文中详细介绍了如何使用动态规划(DP)来解决该问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门


Cake

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

题意:先判断这些点组成的图形是不是一个凸包,如果不是就输出I can't cut. 否则就进行如下计算。

把这些凸包要全部分成三角形。也就是要连n-3条边,并且每切一刀(添一条边有一个花费的计算。|xi+xj|*|yi+yj|%p,

输出最小的花费。

idea:都说了最小的花费了,肯定是用DP先局部找最优解,最后输出最小值。

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

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define LL long long
#define me(x,y) memset(x,y,sizeof(x));
#define bug printf("***********\n")
using namespace std;
const int mod=1e9+7;
const int maxn=1e4+7;
const int maxx=1e3+100;
const int INF=1<<30;

int n,t,cost[maxx][maxx],dp[maxx][maxx];
struct node
{
    int x,y;
}p[maxn],P[maxn];// p[] 存点 P[] 存凸包
int tot;//tot 凸包的点
int X(node A,node B,node C)//差积 是否<0
{
    return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);
}

double len(node A,node B)//距离
{
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

bool cmp(node A,node B)
{
    double pp=X(p[0],A,B);
    if(pp>0)  return 1;
    if(pp<0)  return 0;
    return len(p[0],A)<len(p[0],B); //如果等于0 判断距离  返回小的
}
int main()
{
    while(cin>>n>>t)
    {
        for(int i=0;i<n;i++)
            cin>>p[i].x>>p[i].y;
        if(n<=2)
        {
            printf("I can't cut.\n");
            continue;
        }
        else
        {
             for(int i=0;i<n;i++)
            {
                if(p[i].y<p[0].y)  swap(p[i],p[0]);//找出 y轴最小的点
                else if(p[i].y==p[0].y&&p[i].x<p[0].x)  swap(p[i],p[0]);//x坐标小的
            }
            sort(p+1,p+n,cmp);
            P[0]=p[0];
            P[1]=p[1];
            tot=1;
            for(int i=2;i<n;i++)
            {
                while(tot>0&&X(P[tot-1],P[tot],p[i])<=0) tot--;// P[tot-1]上一个点
                tot++;
                P[tot]=p[i];//存入凸包
            }
            tot++;
            if(tot!=n)
            {
                printf("I can't cut.\n");
                continue;
            }
            else
            {
                me(cost,0);
                for(int i=0;i<n;i++)
                    for(int j=i+2;j<n;j++)
                    {
                        if(i==j) continue;
                        cost[i][j]=cost[j][i]=(int)fabs(p[i].x+p[j].x)
                        *(int)fabs(p[i].y+p[j].y)%t;
                    }
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                        dp[i][j] = INF;
                }
                for (int i = n - 3; i >= 0; i--)
                    for (int j = i + 2; j < n; j++)
                        for (int 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]);
            }
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值