Tree Construction - HDU 3516 四边形不等式优化
2015-06-09 18:30
316 查看
Tree Construction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 941 Accepted Submission(s): 510
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.
![](http://acm.hdu.edu.cn/data/images/C281-1009-1.jpg)
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
题意:用一棵树把所有的点都连接起来。求最小的所需线段的长度和。
思路:设dp[i][j]为合并从i到j的点所需的最小权值。
dp[i][j]=min(dp[i][k]+dp[k+1][j]+x[k+1]-x[i]+y[k]-y[j])。
然后利用四边形不等式去优化。
AC代码如下:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; int T,t,n,m; int dp[1010][1010],s[1010][1010],x[1010],y[1010],INF=1e9; int main() { int i,j,k,ret,len; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]); for(i=1;i<=n;i++) s[i][i]=i-1; for(len=1;len<n;len++) for(i=1;i+len<=n;i++) { j=i+len; dp[i][j]=INF; for(k=s[i][j-1];k<=s[i+1][j];k++) { ret=dp[i][k]+dp[k+1][j]+x[k+1]-x[i]+y[k]-y[j]; if(ret<dp[i][j]) { dp[i][j]=ret; s[i][j]=k; } } } printf("%d\n",dp[1] ); } }
相关文章推荐
- Monkey测试命令的使用
- 链表中倒数第K个结点
- Linux中如何搭建一个ftp服务服务器-超详细
- 动态规划
- BZOJ 3233 Ahoi2013 找硬币 动态规划
- java_eclipse_设置全局编码_utf-8_编译class指定_运行jar乱码解决_不依赖环境
- SpringMvc 异常处理特解
- android获得屏幕高度和宽度
- 动态规划问题
- maya undo chunk
- Monkey Party - HDU 3506 四边形不等式优化
- MATLAB7.0学习之一到运筹学的实验题解答
- webbench压力测试工具
- Android studio设置背景图片
- 通过微信分享链接地址会自动添加from参数区分来源
- mPackageManager.setApplicationHiddenSettingAsUser
- const和define的区别
- VS快捷键大全(转)
- iOS动画
- POJ 2823 Sliding Window滑窗