题目链接:http://acm.uestc.edu.cn/#/problem/show/1653
题意:中文题面
解法:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
} p[1010];
int n, dp[1010][1010], s[1010][1010];
bool cmp(node a, node b)
{
if(a.x==b.x) return a.y>b.y;
return a.x<b.x;
}
int main()
{
while(~scanf("%d", &n))
{
for(int i=1; i<=n; i++) scanf("%d %d", &p[i].x,&p[i].y);
sort(p+1,p+n+1,cmp);
memset(dp, 0x3f, sizeof(dp));
for(int i=1; i<=n; i++) s[i][i]=i,dp[i][i]=0;
for(int len = 2; len <= n; len++)
{
for(int l = 1; l+len-1<=n; l++)
{
int r = l+len-1;
for(int j=s[l][r-1]; j<=s[l+1][r]; j++)
{
if(j!=r)
{
if(dp[l][j]+dp[j+1][r]+p[j+1].x-p[l].x+p[j].y-p[r].y<dp[l][r])
{
dp[l][r]=dp[l][j]+dp[j+1][r]+p[j+1].x-p[l].x+p[j].y-p[r].y;
s[l][r]=j;
}
}
}
}
}
printf("%d\n", dp[1][n]);
}
return 0;
}