HDOJ 3516 Tree Construction
来源:互联网 发布:linux mysql 建表 编辑:程序博客网 时间:2024/06/05 21:07
四边形优化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.
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
51 52 43 34 25 1110000 0
Sample Output
120
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;}
0 0
- HDOJ 3516 Tree Construction
- 【DP】 HDOJ 3516 Tree Construction
- hdu 3516 Tree Construction
- Tree Construction
- 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 (四边形优化)
- HDOJ 5813 Elegant Construction
- hdu3516 Tree Construction
- codeforces round353 tree construction
- 二叉排序树--cf675d Tree construction
- HDU 3516 Tree Construction (四边形不等式优化DP)
- HDU 3516 Tree Construction (dp+四边形不等式优化)
- HDU-3516 Tree Construction(区间dp+四边形优化)
- Sublime Text 2 常使用操作
- VMware 虚拟机中添加新硬盘的方法
- avl树 golang实现
- 为啥程序员要学这么多技术啊?
- InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
- HDOJ 3516 Tree Construction
- UI-关灯游戏,无尽版-贞姐原创
- python脚本
- 黑马程序员—再发一封黑马三期女同学发来的感谢信!
- 求两个字符串的最大公共子串 ,不区分大小写
- 【LeetCode】Pascal's Triangle II
- eclipse常用插件的安装
- I2S音频总线学习(二)I2S总线协议
- 61条面向对象设计的经验原则