HDU 2829 Lawrence(动态规划-四边形不等式)
Lawrence
version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".
You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British Intelligence has assigned
a Strategic Importance to each depot---an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values
for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:

Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.
Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves---they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked
this rail line right in the middle:

The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.
Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.
from 1 to 100, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.
4 1
4 5 1 2
4 2
4 5 1 2
0 0
17
2
题目大意:
有n个点连在一起,m个炸弹能够阻断它们的相连,问你所实用完炸弹后的最小值。
解题思路:
四边形不等式是一种比較常见的优化动态规划的方法:设m[i,j]表示动态规划的状态量。m[i,j]有类似例如以下的状态转移方程:m[i,j]=opt{m[i,k]+m[k,j]}(i≤k≤j)假设对于随意的a≤b≤c≤d,有m[a,c]+m[b,d]≤m[a,d]+m[b,c],那么m[i,j]满足四边形不等式。以上是适用这样的优化方法的必要条件对于一道详细的题目,我们首先要证明它满足这个条件,一般来说用数学归纳法证明,依据题目的不同而不同。通常的动态规划的复杂度是O(n^3),我们能够优化到O(n^2)设s[i,j]为m[i,j]的决策量,即m[i,j]=m[i,s[i,j]]+m[s[i,j],j]我们能够证明,s[i,j-1]≤s[i,j]≤s[i+1,j]对于这题:
转移方程dp[i][j]=min(dp[i-1][k]+cost[k+1][j])(i-1<k<j),cost[i][j+1]-cost[i][j]>0 满足四边形不等式优化的条件。
解题代码:
#include <iostream>
#include <cstdio>
using namespace std; typedef long long ll; const int maxn=1100;
ll cost[maxn][maxn],dp[maxn][maxn],a[maxn];
int n,m,s[maxn][maxn]; void input(){
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=n;i++){
ll sum=0;
cost[i][i]=0;
for(int j=i+1;j<=n;j++){
sum+=a[j-1];
cost[i][j]=cost[i][j-1]+sum*a[j];
}
}
for(int i=0;i<=n;i++){
dp[0][i]=cost[1][i];
s[0][i]=0;
s[i][n+1]=n;
}
} ll solve(){
for(int i=1;i<=m;i++){
for(int j=n;j>=1;j--){
dp[i][j]=1e18;
for(int k=s[i-1][j];k<=s[i][j+1];k++){
if(dp[i-1][k]+cost[k+1][j]<dp[i][j]){
dp[i][j]=dp[i-1][k]+cost[k+1][j];
s[i][j]=k;
}
}
}
}
cout<<dp[m][n]<<endl;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF && (m||n) ){
input();
solve();
}
return 0;
}
HDU 2829 Lawrence(动态规划-四边形不等式)的更多相关文章
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- hdu 2829 Lawrence(四边形不等式优化dp)
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
- HDU 2829 Lawrence(四边形优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HDU 3516 Tree Construction (四边形不等式)
题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 思路:考虑用区间表示,f[i][j]表示将i到j的点连起来的 ...
- HDU.2829.Lawrence(DP 斜率优化)
题目链接 \(Description\) 给定一个\(n\)个数的序列,最多将序列分为\(m+1\)段,每段的价值是这段中所有数两两相乘的和.求最小总价值. \(Solution\) 写到这突然懒得写 ...
- HDU 2829 - Lawrence - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 T. E. Lawrence was a controversial figure during ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HDU 2829 Lawrence
$dp$,斜率优化. 设$dp[i][j]$表示前$i$个数字切了$j$次的最小代价.$dp[i][j]=dp[k][j-1]+p[k+1][i]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...
随机推荐
- OCP-1Z0-051-题目解析-第33题
33. You want to create an ORD_DETAIL table to store details for an order placed having the following ...
- C++学习之路—继承与派生(四)拓展与总结
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1 拓展部分 本节主要由两部分内容组成,分 ...
- org.apache.jasper.JasperException: java.lang.ClassCastException
异常信息: org.apache.jasper.JasperException: java.lang.ClassCastException:org.apache.catalina.util.Defau ...
- HTTPClient和URLConnection核心区别分析
首先:在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection.但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活. 在An ...
- asp.net mvc 导出表格
适合使用的场合: .net 中从前台中的table导出成excel文件,兼容各种浏览器. 使用工具: org.in2bits.MyXls.dll 从前台获取表格的thead和表格的tbody,将其转化 ...
- Raspberry pi raspbain系统下使用vim
一开始 apt-get install vim不好用. 在putty中执行这条命令就可以了. sudo apt-get update && sudo apt-get install v ...
- Swift - whose view is not in the window hierarchy 问题解决方法
问题现象:想在页面初始化的时候,使用self.presentViewController方法弹出个告警提示框UIAlertController.但行后报了个如下告警,同时告警框也出不来. 1 2015 ...
- QT源码分析(从QApplication开始)
QT源码分析 转载自:http://no001.blog.51cto.com/1142339/282130 今天,在给同学讲东西的时候,谈到了Qt源代码的问题,才发现自己对Qt机制的了解是在太少了,而 ...
- 广东省-IT公司红黑榜排名
红榜Top100 Order Company Name Point Change 1 百富计算机技术(深圳)有限公司 94.00 -- 2 中国网通广州分公司 88.00 -- 3 深圳市汇 ...
- Java经典23种设计模式之创造型模式(一)
设计模式被称为程序猿的内功,之前零零散散的看过一大部分,但自己么有总结过.故此次在这里总结下.值得一提的是,设计模式并不是Java所特有.由于一直搞Android.这里就用Java为载体.最经典的设计 ...