四边形优化DP

5 1 5 2 4 3 3 4 2 5 1 1 10000 0
12 0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1100;
const int INF=0x3f3f3f3f;
struct POINT
{
	int x,y;
}pt[maxn];
int n;
int dp[maxn][maxn],s[maxn][maxn];
inline int cost(int i,int j,int k)
{
	return pt[k].y-pt[j].y+pt[k+1].x-pt[i].x;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&pt[i].x,&pt[i].y);
		}
		for(int i=1;i<=n;i++)		
		{
			s[i][i]=i;
		}
		for(int len=2;len<=n;len++)
		{
			for(int i=1;i+len-1<=n;i++)
			{
				int j=i+len-1;
				dp[i][j]=INF;
				for(int k=s[i][j-1];k<=s[i+1][j]&&k<j;k++)
				{
					if(dp[i][j]>dp[i][k]+dp[k+1][j]+cost(i,j,k))
					{
						s[i][j]=k;
						dp[i][j]=dp[i][k]+dp[k+1][j]+cost(i,j,k);
					}
				}
			}
		}
		printf("%d\n",dp[1][n]);
	}
	return 0;
}
原文:http://blog.csdn.net/ck_boss/article/details/38831653