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]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...
随机推荐
- Freemarker概念简单介绍
Freemarker概念简单介绍 1. Freemarker是什么 模板引擎:一种基于模板的,用来生成输出文本的通过工具. 基于java开发包和类库 2. Freemarker能做什么 MVC ...
- wift - 使用UIScreen类获取屏幕大小尺寸
UISreen类代表了屏幕,开发中一般用来获取屏幕相关的属性,例如获取屏幕的大小. 1 2 3 4 5 6 7 //获取屏幕大小 var screenBounds:CGRect = UIScreen. ...
- <转载>使用css让大图片不超过网页宽度
让大图片不超过网页宽度,让图片不撑破通过CSS样式设置的DIV宽度! 接下来,我们来介绍下网站在开发DIV+CSS的时候会遇到一个问题,在发布一个大图片的时候因为图片过宽会撑破自己设置的div宽度的问 ...
- 2007LA 3902 网络(树+贪心)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=283&am ...
- TCP/IP笔记 二.网络层(1)
1. IP 1.1 配套协议 IP 是 TCP/IP 体系中两个最主要的协议之一 . 与 IP 协议配套使用的还有四个协议: (1)ARP (Address Resolution Protocol ...
- 基于JVM规范的并发编程解决方案
在并发的世界里,选择合适的状态处理方法将对并发性和正确性起到决定性的影响.这方面可选的方法有:共享可变性.隔离可变性以及完全不可变性. 对于并发问题来说最好的解决方法是从根本上消灭它而不是花很多时间解 ...
- 【自由谈】城域网IPv6过渡技术——MAP技术(4)
本节接着回答MAP技术的第三个问题:“MAP-BR的Pool是如何实现?可靠性如何提升?” 在MAP域中通过将多个MAP-BR放在同一个Pool内实现负载分担和保护倒换的.同一个Pool中的每个MAP ...
- VC 获取指定文件夹路径的方法小结
VC获取指定文件夹路径 flyfish 2010-3-5 一 使用Shell函数 1 获取应用程序的安装路径 TCHAR buf[_MAX_PATH];SHGetSpecialFolderPath( ...
- tomcat path设置
zjtest7-app:/usr/local/apache-tomcat-7.0.55_8082/logs# netstat -nap | grep 8082 tcp 0 0 :::8082 :::* ...
- BZOJ 2096([Poi2010]Pilots-单调队列-差值)
2096: [Poi2010]Pilots Time Limit: 30 Sec Memory Limit: 162 MB Submit: 190 Solved: 97 [ Submit][ ...