HDOJ 3516 Tree Construction

四边形优化DP

Tree Construction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 868    Accepted Submission(s): 470

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.

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

Source

2010
ACM-ICPC Multi-University Training Contest(8)——Host by ECNU

#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;
}
时间: 2024-10-25 13:31:12

HDOJ 3516 Tree Construction的相关文章

HDOJ 3516 Tree Construction 四边形优化dp

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿距离最短 题解: 很容易得到转移方程: $$dp[i][j]=min \{ dp[i][k-1]+dp[k][j] + dis(uni(i,k-1),uni(k,j))\}$$ 其中$dp[i][j]$表示从$i$到$j$的最优解,$dis(i,j)$表示$i$和$j$之间的曼哈顿距离,$uni(i

【HDU】3516 Tree Construction

http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意:平面n个点且满足xi<xj, yi>yj, i<j.xi,yi均为整数.求一棵树边只能向上和向右延展的经过所有点的最小长度.(n<=1000, 0<=xi, yi<=10000) #include <cstdio> using namespace std; const int N=1005, oo=~0u>>1; int d[N][N], x[N]

HDU 3516 Tree Construction

区间$dp$,四边形优化. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue

【四边形】 HDU 3516 Tree Construction

通道 题意:二维坐标上的点,建一个长度和最小的树包含全部点 思路: 定义状态 dp[i,j]表示点i到点j合并在一起的最小花费(树枝的长度), 状态转移方程:dp[i,j]= min(dp[i,k]+dp[k+1,j]+cost(i,j) ) i<k<j cost(i,j)=py[k]-py[j]+px[k+1]-px[i]; 当j固定时,cost(i,j)单调递减函数 我们猜测cost(i,j)满足四边形不等式 证明: F(i)=cost(i,j+1)-cost(i,j)=py[j]-py[

【HDOJ】【3516】Tree Construction

DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[k+1][j-(k-i+1)]+w(i,k,j)} (这个地方一开始写错了……) 即,将一棵树从k处断开成(i,k)和(k+1,i+j-1)两棵树,再加上将两棵树连起来的两条树枝的长度w(i,k,j) 其中,$ w(i,k,j)=x[k+1]-x[i]+y[k]-y[i+j-1] $ 那么根据四边形

数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

Tree Construction Problem's Link ---------------------------------------------------------------------------- Mean: 给定n个数,按照构造Binary Search Tree的方式来构造BST树,按顺序输出每一个非root结点的父节点的值. analyse: 构造BST树最坏情况下时间复杂度为O(n),肯定会超时. 注意到只需要输出结点的父节点的值,不需要真的构造BST树. 插到第i

HDU 3516 DP 四边形不等式优化 Tree Construction

设d(i, j)为连通第i个点到第j个点的树的最小长度,则有状态转移方程: d(i, j) = min{ d(i, k) + d(k + 1, j) + p[k].y - p[j].y + p[k+1].x - p[i].x } 然后用四边形不等式优化之.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <

HDOJ 2682 Tree(最小生成树prim算法)

Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1954    Accepted Submission(s): 573 Problem Description There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities

HDOJ 5044 Tree

树链剖分裸题.... 又要扩栈又要输入挂还卡格式....真无语 Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1538    Accepted Submission(s): 261 Problem Description You are given a tree (an acyclic undirected connect