Tree Construction
Problem Description
Consider a two-dimensional space with a set of points (xi, yi) that satisfy xi < xj and yi > yj for all i < j. We want to have them all connected by a directed tree whose edges go toward either right (x positive) or upward (y positive). The figure below shows an example tree.
Write a program that finds a tree connecting all given points with the shortest total length of edges.
![](https://i-blog.csdnimg.cn/blog_migrate/250971ecb3527f9d8319f947184c7f1f.jpeg)
Write a program that finds a tree connecting all given points with the shortest total length of edges.
Input
The input begins with a line that contains an integer n (1 <= n <= 1000), the number of points. Then n lines follow. The i-th line contains two integers xi and yi (0 <= xi, yi <= 10000), which give the coordinates of the i-th point.
Output
Print the total length of edges in a line.
Sample Input
5 1 5 2 4 3 3 4 2 5 1 1 10000 0
Sample Output
12 0
一道典型的四边形不等式优化。
当f[i,j]满足f[i,j]+f[i',j']<=f[i',j]+f[i,j'],i<=i'<=j<=j'时,称f满足四边形不等式;
当f[i,j]满足f[i',j]<=f[i,j']时,称f关于区间包含关系单调。
如果状态转移方程为m[i,j]=min{m[i,k-1]+m[k,j]|i<k<=j}+w[i,j],
且w[i,j]为区间包含关系的单调函数,且满足四边形不等式,则m[i,j]也满足四边形不等式,
进一部推出s[i,j-1]<=s[i,j]<=s[i+1,j]。
于是m[i,j]=min{m[i,k-1],m[k,j]|s[i,j-1]<=k<=s[i+1,j]}+w[i,j]。
证明附上国家队毛子青的论文:ACM国家集训队论文集(依据内容)\5、动态规划\《动态规划算法的优化技巧》(由于不能上传文件,将论文放在了百度云上)。
最后,附上代码,希望大家学习愉快
#include<cstdio>
#include<cstring>
const int INF=0x3f3f3f3f;
int dp[1005][1005];
int s[1005][1005];
int x[1005];
int y[1005];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=1; i<=n; i++)
s[i][i]=i;
for(int len=2; len<=n; len++)
for(int i=1; i<=n-len+1; i++)
{
int j=i+len-1;
dp[i][j]=INF;
for(int k=s[i][j-1];k<=s[i+1][j];k++)
{
int ans=dp[i][k-1]+dp[k][j]+x[k]-x[i]+y[k-1]-y[j];
if(dp[i][j]>ans)
{
dp[i][j]=ans;
s[i][j]=k;
}
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}