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

这题要注意树的左端点必定在左上端点向下做垂线和右下端点向左作垂线的交点,思路和石子合并差不多,需要用四边形优化。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 999999999
int x[1006],y[1006],dp[1006][1006],s[1006][1006];
int dis(int x1,int y1,int x2,int y2){
return abs(x1-x2)+abs(y1-y2);
} int main()
{
int n,m,i,j,len,k;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
dp[i][i]=0;
}
for(i=1;i<=n-1;i++){
s[i][i+1]=i;
dp[i][i+1]=dis(x[i],y[i],x[i+1],y[i+1]);
}
for(len=3;len<=n;len++){
for(i=1;i+len-1<=n;i++){
j=i+len-1;
dp[i][j]=inf; for(k=s[i][j-1];k<=s[i+1][j];k++){
if(dp[i][j]>dp[i][k]+dp[k+1][j]+abs(y[j]-y[k])+abs(x[i]-x[k+1]) ){
dp[i][j]=dp[i][k]+dp[k+1][j]+abs(y[j]-y[k])+abs(x[i]-x[k+1]);
s[i][j]=k;
}
}
}
}
printf("%d\n",dp[1][n]); }
return 0;
}

hdu3516 Tree Construction的更多相关文章

  1. hdu3516 Tree Construction (区间dp+四边形优化)

    构造方法肯定是把相邻两个点连到一起,变成一个新点,然后再把新点和别的点连到一起.... 设f[i,j]为把第i到j个点都连到一起的代价,那么答案就是f[1,n] f[i,j]=min{f[i,k]+f ...

  2. [HDU3516] Tree Construction [四边形不等式dp]

    题面: 传送门 思路: 这道题有个结论: 把两棵树$\left[i,k\right]$以及$\left[k+1,j\right]$连接起来的最小花费是$x\left[k+1\right]-x\left ...

  3. hdu3516 Tree Construction (四边形不等式)

    题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 题解:直接给出吧 f[i][j]=min(f[i][k]+f ...

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

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

  5. codeforces 675D D. Tree Construction(线段树+BTS)

    题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...

  6. HDOJ 3516 Tree Construction

    四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  8. CF 675D——Tree Construction——————【二叉搜索树、STL】

    D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. STL---Codeforces675D Tree Construction(二叉树节点的父亲节点)

    Description During the programming classes Vasya was assigned a difficult problem. However, he doesn ...

随机推荐

  1. python函数3-函数嵌套/递归/匿名函数

    2 .函数递归: 3.匿名函数

  2. 【Git】Git初始化一个仓库

    文章目录 初始化仓库 检查当前文件状态 跟踪新文件 提交更新 跳过使用暂存区域 移除文件 添加远程仓库 推送到远程仓库 简单记录-慕课网 从0开始 独立完成企业级Java电商网站开发 Git初始化一个 ...

  3. 【Nginx】配置nginx图片服务器

    想通过nginx来访问服务器上的图片 可以搭建一个nginx图片服务器. 做法如下: 先安装nginx,这里直接用yum来进行安装的 安装方法如下: https://blog.csdn.net/iml ...

  4. kubernets之Replication Controller

    一  Replication Controller的介绍      pod可能会由于各种原因消失和多出来,例如node节点去除集群或者人为的手工创建,所以为了方便和管理pod的数量,k8s里面 的另外 ...

  5. ctfshow—pwn10

    格式化字符串漏洞 具体什么是格式化字符串请大家参考如下文章 https://wiki.x10sec.org/pwn/fmtstr/fmtstr_intro/ printf函数格式化输出符号及详细说明 ...

  6. 通过LOGMNR查找程式带入的实际值

    生产库中出现了大量的锁表,需要得到当时程式执行的SQL以及其带入的值 1.查看SQL SELECT SQL_ID FROM V$SESSION WHERE SID=(SELECT FINAL_BLOC ...

  7. js模仿京东首页的倒计时功能

    模仿京东首页的倒计时 我们学习了定时器,可以用定时器做一个倒计时,于是我模仿了京东首页倒计时. 先看看京东首页的倒计时. 思路: 如何倒计时? 给一个未来的时间.然后计算未来时间到现在的时间戳. 用定 ...

  8. Mybatis plus 报错Invalid bound statement (not found) 终极解决办法

    我产生的错误原因是写的mapper继承BaseMapper没有添加泛型: 点进去: 为了解决这个bug,网上很多人也提出了解决办法:1.检查xml文件的namespace是否正确 2.Mapper.j ...

  9. go 语言开发中 GOPATH问题 与 go语言linux 开发环境 教程

    https://github.com/rubyhan1314/Golang-100-Days/blob/master/Day01-15(Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7 ...

  10. WPF和MVVM的结合使用方法,不可错过

    Model:存储数据模型(类) 也在此业务逻辑,主要负责类文件的存储. ViewModel:连接View和Model,借助Command来负责界面的跳转和调用Model中方法来操作Model的数据. ...