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.
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.
If the cake is not convex polygon-shaped, output "I can't cut.". Otherwise, output the minimum cost.
3 3 0 0 1 1 0 2
0
题意:给定n个点的坐标,先问这些点是否能组成一个凸包,如果是凸包,问用不相交的线来切这个凸包使得凸包只由三角形组成,根据costi, j = |xi + xj| * |yi + yj| % p 算切线的费用,问最少的切割费用。听说这个是最优三角剖分?嗯,不得了。
判断凸包就是普通的板子,极角排序判断叉积是不是方向相同。
嗯,求最小值,看他们都叫做最优三角剖分,就是类似一个区间dp吧。没想到区间dp还可以这样玩。实际上就是找一个过渡点k,看是否能更新dp[i][j]的值。
这个题还可以记忆化搜索。
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int inf = 1e9;
const int MAXN = 300+7;
int n,m;
int cost[MAXN][MAXN];
int dp[MAXN][MAXN];
struct Point
{
int x,y;
}p[MAXN],p0;
//a,b距离的平方
int dis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
//计算叉积
int det(int x1,int y1,int x2,int y2)
{
int d = x1*y2-x2*y1;
if(d == 0)return 0;
return d > 0?1:-1;
}
//ab叉乘cd
int cross(Point a,Point b,Point c,Point d)
{
return det(b.x-a.x,b.y-a.y,d.x-c.x,d.y-c.y);
}
//极角排序
bool cmp(Point a,Point b)
{
int temp = cross(p0,a,p0,b);
if(temp)return temp > 0;
else return dis(p0,a) < dis(p0,b);
}
//判断凸包
bool check_tubao()
{
int d = 0,temp;
for(int i = 0 ; i < n ;++i)
{
temp = cross(p[i],p[i+1],p[i+1],p[i+2]);
if(!d)d = temp;
if(d*temp <= 0)return 0;
}
return 1;
}
//ab两点连线的代价
int calc(Point a,Point b)
{
return abs(a.x + b.x)*abs(a.y + b.y)%m;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
p0.x = inf;
for(int i = 0 ; i < n ;++i)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[i].x < p0.x)p0 = p[i];
else if(p[i].x == p0.x && p[i].y < p0.y)p0 = p[i];
}
sort(p,p+n,cmp);
p[n] = p[0];
p[n+1] = p[1];
if(!check_tubao())puts("I can't cut.");
else
{
//否则区间dp求最小值
for(int i = 0 ; i < n ;++i)
for(int j = i + 2 ; j < n ;++j)cost[i][j] = cost[j][i] = calc(p[i],p[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)
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] + cost[i][k] + cost[k][j]);
}
printf("%d\n",dp[0][n-1]);
}
}
}
记忆化搜索:
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int inf = 1e9;
const int MAXN = 300+7;
int n,m;
int cost[MAXN][MAXN];
int dp[MAXN][MAXN];
struct Point
{
int x,y;
}p[MAXN],p0;
//a,b距离的平方
int dis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
//计算叉积
int det(int x1,int y1,int x2,int y2)
{
int d = x1*y2-x2*y1;
if(d == 0)return 0;
return d > 0?1:-1;
}
//ab叉乘cd
int cross(Point a,Point b,Point c,Point d)
{
return det(b.x-a.x,b.y-a.y,d.x-c.x,d.y-c.y);
}
//极角排序
bool cmp(Point a,Point b)
{
int temp = cross(p0,a,p0,b);
if(temp)return temp > 0;
else return dis(p0,a) < dis(p0,b);
}
//判断凸包
bool check_tubao()
{
int d = 0,temp;
for(int i = 0 ; i < n ;++i)
{
temp = cross(p[i],p[i+1],p[i+1],p[i+2]);
if(!d)d = temp;
if(d*temp <= 0)return 0;
}
return 1;
}
//ab两点连线的代价
int calc(Point a,Point b)
{
return abs(a.x + b.x)*abs(a.y + b.y)%m;
}
int dfs(int l,int r)
{
if(dp[l][r] != inf)return dp[l][r];
int ans = inf;
for(int i = l + 1 ; i < r; ++i)
{
ans = min(ans,dfs(l,i) + dfs(i,r) + cost[l][i] + cost[i][r]);
}
return dp[l][r] = ans;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
p0.x = inf;
for(int i = 0 ; i < n ;++i)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[i].x < p0.x)p0 = p[i];
else if(p[i].x == p0.x && p[i].y < p0.y)p0 = p[i];
}
sort(p,p+n,cmp);
p[n] = p[0];
p[n+1] = p[1];
if(!check_tubao())puts("I can't cut.");
else
{
//否则区间dp求最小值
for(int i = 0 ; i < n ;++i)
for(int j = i + 2 ; j < n ;++j)cost[i][j] = cost[j][i] = calc(p[i],p[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;
}
printf("%d\n",dfs(0,n-1));
}
}
}
感觉记忆化搜索技能又提升了~