四边形优化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.
 
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
 
Source
 

#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;
}

HDOJ 3516 Tree Construction的更多相关文章

  1. HDOJ 3516 Tree Construction 四边形优化dp

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...

  2. 【HDU】3516 Tree Construction

    http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意:平面n个点且满足xi<xj, yi>yj, i<j.xi,yi均为整数.求一棵树边 ...

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

    题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 思路:考虑用区间表示,f[i][j]表示将i到j的点连起来的 ...

  4. HDU.3516.Tree Construction(DP 四边形不等式)

    题目链接 贴个教程: 四边形不等式学习笔记 \(Description\) 给出平面上的\(n\)个点,满足\(X_i\)严格单增,\(Y_i\)严格单减.以\(x\)轴和\(y\)轴正方向作边,使这 ...

  5. HDU 3516 Tree Construction

    区间$dp$,四边形优化. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio&g ...

  6. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

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

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

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

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

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

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

随机推荐

  1. 性能测试之LoardRunner 检查点

    概述 1.检查点概念 2.实例 以下是详细介绍 检查点:首先来看一下VuGen确定脚本运行成功的判断条件.在录制编写脚本后,通常就会进行回放,如果回放通过没有错误,就认为脚本是正确的.究竟VuGen怎 ...

  2. python 获取当前日期 星期

    from datetime import datetime d =datetime.today()     #获取当前日期时间 d.isoweekday()           #获取时间周几

  3. Mac AppStore 登陆提示 未知错误

    Mac Book 登陆的时候提示未知错误,刚开始以为是需要内建网卡后来才得知: 如果是黑苹果,或者是网卡没有对应上的,需要内建网卡 mac 系统默认的是无线网卡en0 其他都网卡 以此类推 如果是ma ...

  4. .net三步配置错误页面,让你的站点远离不和谐的页面

    假设你的站点出现一堆让人看不懂的报错,那么你就不是一个合格的程序猿.也不是一个合格的站长. 以下的方面能够帮助你的站点远离让人头大的页面. 第一步:配置web.config 打开web.config, ...

  5. 关于iOS7以后版本号企业公布问题

    大家都知道,苹果在公布7.1以后,不打个招呼就把企业公布方式给换掉了(谴责一下~) 曾经普通server+web页面+ipa+plist就能够搞定,如今已经不行了. 关于如今企业公布教程网上贴出来了非 ...

  6. Android 3d云标签

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWluZ3l1ZV8xMTI4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  7. 【翻译】十大要避免的Ext JS开发方法

    原文地址:http://www.sencha.com/blog/top-10-ext-js-development-practices-to-avoid/ 作者:Sean Lanktree Sean ...

  8. vs2005及以上版本的程序分发问题

    我们使用vs2005及以上版本编译的应用程序(C/C++),在客户机器运行时,会出现: “由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题” 那么,我们怎么解决这个问题呢 ...

  9. GCC编译优化指南【作者:金步国】

    GCC编译优化指南[作者:金步国] GCC编译优化指南 作者:金步国 版权声明 本文作者是一位自由软件爱好者,所以本文虽然不是软件,但是本着 GPL 的精神发布.任何人都可以自由使用.转载.复制和再分 ...

  10. POJ1291-并查集/dfs

    并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...