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]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...
随机推荐
- IOS 应用的架构解析
首先新建一个IOS 的应用工程,主要讲解一下的文件组成: main.m XXXXDelegete.h\.m MainWindow.xib info.plist 文件 IOS 应用程序由UIKit 封装 ...
- HDU 4691(多校第九场1006) 后缀数组
...还能多说什么. 眼角一滴翔滑过. 一直以为题意是当前串与所有之前输入的串的LCP...然后就T了一整场. 扫了一眼标程突然发现他只比较输入的串和上一个串? 我心中突然有千万匹草泥马踏过. 然后随 ...
- 关于vs2005 __RPC__out __RPC__in 没有定义编译错误
1. 下载WDK http://www.microsoft.com/en-us/download/details.aspx?id=11800 2. 安装WDK 3. vs2005 设置:工具--> ...
- 测试关闭mojo utf-8
[root@wx03 ~]# cat test.pl use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; ...
- discuz!代码内置颜色大全(收藏)
加闪烁字:[light]文字[/light] 加文字特效:[shadow=255,red,2]文字[/shadow]: 在标签的中间插入文字可以实现文字阴影特效,shadow内属性依次为宽度.颜色和边 ...
- csdn发博文验证码缺陷
csdn验证码的长处: 一,差点儿没有浪费人脑人力,却要花去机器人非常多cpu csdn发博文验证码却有非常大缺陷: 一.验证码的内容终于结果是简单的数字,能够穷举尽的,也就是说不怕被封号的话全然能够 ...
- T-SQL 操作文件 具体解释
/******* 导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S" ...
- 键盘游戏之canvas--用OO方式写
虽然写的不是很好,但 解释权以及版权仍然归13东倍所有! <!DOCTYPE HTML> <html> <head> <title>canvas-00 ...
- “ASP.default_aspx”并不包括“DropDownList1_SelectedIndexChanged”的定义,其解决方法。
"ASP.default_aspx"并不包括"DropDownList1_SelectedIndexChanged"的定义,其解决方法. 在使用DropDown ...
- 解决Myeclipse在port占用,导致tomcat无法启动。(Linux)
本文来源于:http://blog.csdn.net/svitter 引文:http://www.2cto.com/os/201305/209285.html { ubuntu查看占用某port的程序 ...