和http://www.cnblogs.com/kuangbin/category/475693.html类似 最优三角剖分
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const double eps = 1e-6;
const double INF = 1e18;
int dcmp(double x) {
if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}
struct Point {
double x, y;
Point(double x=0, double y=0):x(x),y(y) { }
void read(){
scanf("%lf%lf", &x, &y);
}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Point A, Point B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator == (const Point& a, const Point &b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double angle(Vector v) { return atan2(v.y, v.x); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
//点在p线段上
bool OnSegment(Point p, Point a1, Point a2) {
return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) <= 0; //线段包含端点时改成<=
}
//线段相交判定
bool SegmentItersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1),
c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
const int N = 110;
Point p[N];
int n;
int isPointInPolygon(Point tmp)
{
int wn = 0;
for (int i = 0; i < n; i++)
{
if (OnSegment(tmp, p[i], p[(i + 1) % n])) return -1; //边界
int k = dcmp(Cross(p[(i + 1) % n] - p[i], tmp - p[i]));
int d1 = dcmp(p[i].y - tmp.y);
int d2 = dcmp(p[(i + 1) % n].y - tmp.y);
if (k > 0 && d1 <= 0 && d2 > 0) wn++;
if (k < 0 && d2 <= 0 && d1 > 0) wn--;
}
if (wn != 0) return 1; //内部
return 0; //外部
}
bool isok[N][N], vis[N][N];
double dp[N][N], len[N][N];
bool ok(int i, int j)
{
if (!isPointInPolygon((p[i] + p[j]) * 0.5)) return false;///线段在多边形外部
for (int r = 0; r < n; r++)
{
if (r != i && r != j)///在内部时
{
if (SegmentItersection(p[r], p[(r + 1) % n], p[i], p[j])) return false;///与其它的边相交(不判端点)
if (OnSegment(p[r], p[i], p[j])) return false;///(判端点)
}
}
return true;
}
void init()
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
{
if (ok(i, j)) isok[j][i] = isok[i][j] = true;
else isok[j][i] = isok[i][j] = false;
len[j][i] = len[i][j] = Length(p[i] - p[j]);
}
}
double dpf(int l, int r)
{
if (r - l <= 2) return 0;
if (vis[l][r]) return dp[l][r];
vis[l][r] = true;
double &ans = dp[l][r];
ans = 1e100;
for (int i = l + 2; i <= r - 2; i++)
{
if (isok[l][i] && isok[i][r])
ans = min(ans, dpf(l, i) + dpf(i, r) + len[l][i] + len[i][r]);
}
if (isok[l + 1][r])
ans = min(ans, dpf(l + 1, r) + len[l + 1][r]);
if (isok[l][r - 1])
ans = min(ans, dpf(l, r - 1) + len[l][r - 1]);
return ans;
}
int main()
{
int nc = 1;
while(~scanf("%d", &n))
{
for(int i = 0; i < n; i++) p[i].read();///题目输入的点是按时针顺序的
init();
CLR(vis, 0);
printf("Case %d: %.4f\n", nc++, dpf(0, n - 1));
}
}