dp四边形优化 Hdu 3516 Tree Construction 题解
来源:互联网 发布:女生性格判断 知乎 编辑:程序博客网 时间:2024/05/17 02:37
累加器传送门:
http://blog.csdn.net/NOIAu/article/details/71775000
题目传送门:
https://vjudge.net/problem/HDU-3516
题目:
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.
输入:
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.
输出:
Print the total length of edges in a line.
样例输入:
5
1 5
2 4
3 3
4 2
5 1
1
10000 0
样例输出:
12
0
题目大意:
二维平面上有n个,这些点满足
i < j时,xi < xj, yi > yj
只用朝x轴或y轴正方向的边连起来,形成一棵树,使得所有边的长度最小
其中 n <= 1000
很容易想到一个dp转移方程dp[i][j]=min{dp[i][k]+dp[k+1][j]+cost[i][k][k+1][j]}
这个cost[i][k][k+1][j]没必要预处理出来
模拟一下可以得到
它的值等于p[k].y-p[j].y+p[k+1].x-p[i].x
所以四边形优化就可以了,这篇博文有四边形优化详细讲解
http://blog.csdn.net/NOIAu/article/details/72514812
代码如下
#include<cstdio>#include<cstring>#include<iostream>#define MAXN 1000+10using namespace std;int n;int s[MAXN][MAXN],dp[MAXN][MAXN];struct P{ int x,y;}p[MAXN];bool init(){ if(scanf("%d",&n)==EOF) return false; for(register int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y); for(register int i=1;i<=n;i++){ dp[i][i]=0; s[i][i]=i; } return true;}void dpp(){ for(register int i=n;i>=1;i--){ for(register int j=i+1;j<=n;j++){ int temp=0x7fffffff; int te; for(int k=s[i][j-1];k<=s[i+1][j];k++){ if(k+1<=j){ if(temp>dp[i][k]+dp[k+1][j]+p[k].y-p[j].y+p[k+1].x-p[i].x){ temp=dp[i][k]+dp[k+1][j]+p[k].y-p[j].y+p[k+1].x-p[i].x; te=k; } } } dp[i][j]=temp; s[i][j]=te; } } cout<<dp[1][n]<<endl;}int main(){ while(init()){ dpp(); } return 0;}
- dp四边形优化 Hdu 3516 Tree Construction 题解
- HDU 3516 Tree Construction (四边形优化DP)
- HDU 3516 Tree Construction (四边形不等式优化DP)
- HDU 3516 Tree Construction (dp+四边形不等式优化)
- HDU-3516 Tree Construction(区间dp+四边形优化)
- hdu 3516 Tree Construction (四边形不等式优化DP)
- hdu 3516 Tree construction DP+四边形不等式
- hdu 3516 Tree Construction(四边形优化)
- Tree Construction - HDU 3516 四边形不等式优化
- HDU - 3516 Tree Construction(四边形优化)
- hdu 3516 Tree Construction 四边形不等式优化
- hdu 3516 Tree Construction (四边形优化)
- dp四边形优化 Hdu 2829 Lawrence 题解
- dp四边形优化 Hdu 3480 Division 题解
- hdu 3516 四边形优化dp
- hdu 四边形优化dp
- hdu 3516 dp+四边形不等式优化
- hdu 3516(四边形优化dp)
- Linux-六-软件包管理-(20170518)
- 你应该掌握的七种回归技术
- 优先队列和堆排序
- 网络基础复习
- VMware虚拟机三种网络模式详解-Bridged(桥接模式)
- dp四边形优化 Hdu 3516 Tree Construction 题解
- 嵌入式系统学习——STM32之跑马灯
- CSS并不简单--走进border、box-shadow和outline
- 期望风险、经验风险与结构风险之间的关系
- 在内存剖析对象
- 【ASP.NET】 IRequiresSessionState接口的使用
- MYSQL性能优化之Mysql数据库结构设计
- Redis的主从复制
- 多重共线性的解决方法之——岭回归与LASSO