原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516

题意:

大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿距离最短

题解:

很容易得到转移方程:

$$dp[i][j]=min \{ dp[i][k-1]+dp[k][j] + dis(uni(i,k-1),uni(k,j))\}$$

其中$dp[i][j]$表示从$i$到$j$的最优解,$dis(i,j)$表示$i$和$j$之间的曼哈顿距离,$uni(i,j)$表示将$i$和$j$用平行于坐标轴的线段连在一起时的拐角点。

然后可以证明这是满足四边形优化条件的。

然后四边形优化一下就好。

代码:

#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<climits>
#include<cmath>
#define MAX_N 1234
#define INF LLONG_MAX/1234
using namespace std; typedef long long LL; LL dp[MAX_N][MAX_N]; int n; struct point {
public:
LL x, y; point(LL xx, LL yy) : x(xx), y(yy) { } point() { }
}; point P[MAX_N]; LL dis(point a,point b){
return abs(a.x-b.x)+abs(a.y-b.y);
} point uni(point a,point b) {
return point(min(a.x, b.x), min(a.y, b.y));
} int s[MAX_N][MAX_N]; int main() {
cin.sync_with_stdio(false);
while (cin >> n) {
for (int i = ; i <= n; i++)
cin >> P[i].x >> P[i].y;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
if (i == j)
dp[i][j] = ;
else
dp[i][j] = INF;
}
for (int i = ; i < n; i++)
dp[i][i + ] = dis(P[i], P[i + ]), s[i][i + ] = i + ;
for (int t = ; t <= n; t++)
for (int i = ; i + t <= n; i++) {
int j = i + t;
int pos = ;
for (int k = s[i][j - ]; k <= s[i + ][j]; k++)
if (dp[i][j] > dp[i][k - ] + dp[k][j]+ dis(uni(P[i], P[k - ]), uni(P[k], P[j])))
dp[i][j] = dp[i][k - ] + dp[k][j] + dis(uni(P[i], P[k - ]), uni(P[k], P[j])), pos = k;
s[i][j] = pos;
}
cout << dp[][n] << endl;
}
return ;
}

HDOJ 3516 Tree Construction 四边形优化dp的更多相关文章

  1. HDOJ 3516 Tree Construction

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

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

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

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

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

  4. HDU 2829 Lawrence(四边形优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  5. HDU 3516 Tree Construction

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

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

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

  7. hdu2829 四边形优化dp

    Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. 【HDU】3516 Tree Construction

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

  9. 四边形优化dp

    理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...

随机推荐

  1. java初学2

    1.数组操作类Arrays与System public static void arraycopy(Object src, int srcPos, Object dest,int destPos,in ...

  2. spring boot打war包启动Tomcat失败

    Tomcat启动失败:最后一个causy by :java.lang.NoSuchMethodError: org.apache.tomcat.util.res.StringManager.getMa ...

  3. CodeForces-999D Equalize the Remainders

    题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...

  4. hnust py road

    问题 C: Py Road 时间限制: 1 Sec  内存限制: 128 MB提交: 125  解决: 34[提交][状态][讨论版] 题目描述 Life is short,you need Pyth ...

  5. mysql中查询常用的关键字

    最简单的查询: 1 select * from [where ] 1 select column1,column2....from [where] 这里需要注意的是where子句中条件过滤使用到的关键 ...

  6. Windows 下开发.NET Core应用

    一.使用Visual Studio 2015开发1.1 依次安装Visual Studio 2015 Update 3.NET Core 1.0.0 - VS 2015 Tooling Preview ...

  7. Linux中awk后面的RS, ORS, FS, OFS 含义

    转载自http://blog.csdn.net/qq416647781/article/details/40649419   一.RS 与 ORS 差在哪   我们经常会说,awk是基于行列操作文本的 ...

  8. PHP文件操作函数及文件指针理解

    知识点: 一.fopen(),文件打开函数,读写参数有: 1.R  : 只读,指针在文件开头 2.r+:读写,指针同上 3.W :只写,写入前会删除文件内容,然后指针回到文件开头,文件不存在则创建 4 ...

  9. intellij idea导入不了java.util.Date解决办法

    可以在Settings -> Editor -> General -> Auto Import,将Exclude中的java.util.Date删除.

  10. NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet

    springMVC 内嵌jetty时,出现了NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet: 添加了 ...