ZOJ 3537 简单凸包+DP

/*
简单学习了凸包,基本照着代码抄
凸包好像就是排序后跑一个polygon函数,就能得到一个凸包了
原理的话用平面直角坐标系的斜率理解

凸包之后,图上的点被排序成顺时针排列的点
此时就是区间dp的思路了

*/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
#define inf (1000000007)
const int MAXN = 300 + 5;
struct Point
{
    int x, y;
}p[MAXN], sta[MAXN];
bool cmp(Point a, Point b)
{
    if(a.x == b.x) return a.y < b.y;
    else return a.x < b.x;
}
int n, mod;
int valid(Point a, Point b, Point c)
{
    return (a.y - c.y) * (b.x - c.x) - (b.y - c.y) * (a.x - c.x);
}
int polygon()
{
    sort(p, p + n, cmp);
    int cnt = 0;
    for(int i = 0 ; i < n ; i++){
        while(cnt > 1 && valid(sta[cnt], p[i], sta[cnt - 1]) >= 0) cnt--;
        sta[++cnt] = p[i];
    }
    int temp = cnt;
    for(int i = n - 2 ; i >= 0 ; i--){
        while(cnt > temp && valid(sta[cnt], p[i], sta[cnt - 1]) >= 0) cnt--;
        sta[++cnt] = p[i];
    }
    return cnt;
}
int cost[MAXN][MAXN];
int dp[MAXN][MAXN];
int cal(Point a, Point b) {return (abs(a.x + b.x) % mod * abs(a.y + b.y)) % mod;}
int main()
{
    while(scanf("%d%d", &n, &mod) != EOF){
        for(int i = 0 ; i < n ; i++) scanf("%d%d", &p[i].x, &p[i].y);
        int tot = polygon();
        if(tot <= n) printf("I can't cut.\n");
        else{
            memset(dp, 0, sizeof(dp));
            for(int i = 1 ; i <= n ; i++){
                for(int j = i + 2 ; j <= n ; j++) dp[i][j] = dp[j][i] = inf;
            }
            memset(cost, 0, sizeof(cost));
            for(int i = 1 ; i <= n ; i++){
                for(int j = i + 2 ; j <= n ; j++)
                    cost[i][j] = cost[j][i] = cal(sta[i], sta[j]);
            }
            for(int i = n ; i >= 1 ; 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] + cost[i][k] + dp[k][j] + cost[k][j]);
                }
            }
        printf("%d\n", dp[1][n]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值