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. Deep Learn I'm back.

    Intorduction: 时隔好几个月,我准备重新进入Deep Learning 的领域.昨天和老师聊了很多,之前觉得我做的工作就是排列组合,在水论文,灌水.但老师却说:这也是为将来的研究打基础. ...

  2. python模块详解 | pyquery

    简介 pyquery是一个强大的 HTML 解析库,利用它,我们可以直接解析 DOM 节点的结构,并通过 DOM 节点的一些属性快速进行内容提取. 官方文档:http://pyquery.readth ...

  3. session、cookie、token的区别

    从安全性优先级来说: 1.优先级 Cookie<session<token 2. 安全性 Cookie: ①cookie不是很安全,别人可以分析存放在本地的cookie并进行cookie欺 ...

  4. 【Java】集合框架(List Set Map)

    文章目录 集合框架 List(列表) ArrayList 案例 Set HashSet 案例 iterator(迭代器) Map HashMap 案例 集合总结 参考资料 重新搞一波 复习巩固 简单记 ...

  5. win10打开IIS服务并发布网站

    1.打开控制面板 win+x后点击控制面板 2.点击程序集下边的解除安装程式 3.点击开启或关闭windows功能 4.找到Internet information services并勾选前面的复选框 ...

  6. UI测试框架

    1. 从上到下共分成4层: 用例层  组件管理层  元素管理层  公共数据层 2. 用例层: 将每条用例使用参数化, 公共参数存储到"公共数据层", 中间参数通过组件层传递 3. ...

  7. python的Counter类

    python的Counter类 Counter 集成于 dict 类,因此也可以使用字典的方法,此类返回一个以元素为 key .元素个数为 value 的 Counter 对象集合 from coll ...

  8. pandas高级操作

    pandas高级操作 import numpy as np import pandas as pd from pandas import DataFrame,Series 替换操作 替换操作可以同步作 ...

  9. 使用EFCore连接Oracle数据库时出现的问题

    问题 System.TypeLoadException: Method 'Create' in type 'Oracle.EntityFrameworkCore.Infrastructure.Inte ...

  10. Hive常用日期格式转换

    固定日期转换成时间戳 select unix_timestamp('2016-08-16','yyyy-MM-dd') --1471276800 select unix_timestamp('2016 ...