题目链接:见这里
题意:求将一个凸n多边形分成n-2个三角形的最小费用
分析:这个题首先要判是不是凸包,如果是凸包,那么从逆时针开始枚举分割的起点,和终点,注意起点从n - 3开始,确定起点和终点之后,枚举中间点转移。
<nobr> dp[i][j]代表将凸包的子多边形i j分成(j−i−1)个三角形的最优答案 </nobr>
<nobr> 转移dp[i][j]=max(dp[i][k]+dp[k][j]+cost[i][k]+cost[k][j]) </nobr>
不懂这个可以看这个blog: 见这里
<nobr> 样例只给了组n=3的,真的蛋疼。 </nobr>
代码如下:
//
//Created by BLUEBUFF 2016/1/10
//Copyright (c) 2016 BLUEBUFF.All Rights Reserved
//
#pragma comment(linker,"/STACK:102400000,102400000")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define CLR(a, b) memset(a, b, sizeof(a))
#define MP(x, y) make_pair(x,y)
template <class T1, class T2>inline void getmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void getmin(T1 &a, T2 b) { if (b<a)a = b; }
const int maxn = 310;
const int maxm = 1e5+5;
const int maxs = 10;
const int maxp = 1e3 + 10;
const int INF = 1e9;
const int UNF = -1e9;
const int mod = 1e9 + 7;
const int rev = (mod + 1) >> 1; // FWT
//const double PI = acos(-1);
//head
int m , f[400][400], dp[400][400];
struct Point{
int x, y;
Point(){}
Point(int x, int y) : x(x), y(y){}
void read(){
scanf("%d%d", &x, &y);
}
bool operator < (const Point &rhs) const{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
Point operator - (const Point &rhs) const{
return Point(x - rhs.x, y - rhs.y);
}
};
double Cross(Point A, Point B){
return A.x * B.y - A.y * B.x;
}
int ConvexHull(Point *p, int n, Point *ch){
sort(p, p + n);
int mm = 0;
for(int i = 0; i < n; i++){
while(mm > 1 && Cross(ch[mm - 1] - ch[mm - 2], p[i] - ch[mm - 2]) <= 0) mm--;
ch[mm++] = p[i];
}
int k = mm;
for(int i = n - 2; i >= 0; i--){
while(mm > k && Cross(ch[mm - 1] - ch[mm - 2], p[i] - ch[mm - 2]) <= 0) mm--;
ch[mm++] = p[i];
}
if(n > 1) mm--;
return mm;
}
int calc(Point a, Point b){
return (abs(a.x + b.x) * abs(a.y + b.y)) % m;
}
int n;
Point p[400], ch[400];
//int dfs(int l, int r){
// if(dp[l][r] != -1) return dp[l][r];
// if(r - l <= 2) return 0;
// dp[l][r] = INF;
// for(int k = l + 1; k < r; k++){
// dp[l][r] = min(dp[l][r], dp[l][k] + dp[k][r] + f[l][k] + f[k][r]);
// }
// return dp[l][r];
//}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
memset(p, 0, sizeof(p));
memset(ch, 0, sizeof(ch));
memset(f, 0, sizeof(f));
for(int i = 0; i < n; i++) p[i].read();
if(n == 3){
puts("0"); continue;
}
if(ConvexHull(p, n, ch) < n){
printf("I can't cut.\n");
}
else{
for(int i = 0; i < n; i++){
for(int j = i + 2; j < n; j++){
f[i][j] = f[j][i] = calc(ch[i], ch[j]);
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
dp[i][j] = INF;
dp[i][(i + 1)%n]=0;
}
for (int i = n - 3; i >= 0; i--)//区间dp求解
{
for (int j = i + 2; j < n; j++)
{
for (int k = i + 1; k < j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + f[i][k] + f[k][j]);
}
}
}
printf("%d\n", dp[0][n - 1]);
}
}
return 0;
}