Lawrence

Problem Description
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highly fictionalized
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. 
 
Input
There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each
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.
 
Output
For each data set, output a single integer, indicating the smallest Strategic Value for the railroad that Lawrence can achieve with his attacks. Output each integer in its own line.
 
Sample Input
4 1
4 5 1 2
4 2
4 5 1 2
0 0
 
Sample Output
17
2
 
Source
 

题目大意:

有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(动态规划-四边形不等式)的更多相关文章

  1. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  2. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  3. HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

    题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...

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

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

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

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

  6. HDU.2829.Lawrence(DP 斜率优化)

    题目链接 \(Description\) 给定一个\(n\)个数的序列,最多将序列分为\(m+1\)段,每段的价值是这段中所有数两两相乘的和.求最小总价值. \(Solution\) 写到这突然懒得写 ...

  7. HDU 2829 - Lawrence - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 T. E. Lawrence was a controversial figure during ...

  8. HDU 2829 Lawrence(斜率优化DP O(n^2))

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

  9. HDU 2829 Lawrence

    $dp$,斜率优化. 设$dp[i][j]$表示前$i$个数字切了$j$次的最小代价.$dp[i][j]=dp[k][j-1]+p[k+1][i]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...

随机推荐

  1. MSSQL - 自增1的标识列一次增长了1000

    @情若天_RunUp: 1. Open "SQL Server Configuration Manager"2. Click "SQL Server Services&q ...

  2. 大数据时代的精准数据挖掘——使用R语言

    老师简介: Gino老师,即将步入不惑之年,早年获得名校数学与应用数学专业学士和统计学专业硕士,有海外学习和工作的经历,近二十年来一直进行着数据分析的理论和实践,数学.统计和计算机功底强悍. 曾在某一 ...

  3. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  4. CABasicAnimation学习Demo 包含了一些经常使用的动画效果

    个人写的一些样例: // // ViewController.m // CABasicAnimationDemo // // Created by haotian on 14-6-13. // Cop ...

  5. 4.锁--Synchronizer Framework Base Class—AbstractQueuedSynchronizer介绍

    1. AQS简单介绍 AQS是Java并发类库的基础.其提供了一个基于FIFO队列,可以用于构建锁或者其它相关同步装置的基础框架.该同步器(下面简称同步器)利用了一个int来表示状态,期望它可以成为实 ...

  6. Oracle中如何插入特殊字符:& 和 ' (多种解决方案)

    今天在导入一批数据到Oracle时,碰到了一个问题:Toad提示要给一个自定义变量AMP赋值,一开始我很纳闷,数据是一系列的Insert语句,怎么会有自定义变量呢?后来搜索了一下关键字AMP发现,原来 ...

  7. Java中取某一个范围的随机数

    一.取模操作 public static void main(String[] args) { for (int i = 1; i <= 20; i++) { int j = i % 11; S ...

  8. 查找MobileSafari WebKit revision number的方法

    Mobile Safari是开源的Mac Safari的iOS版本,然而iOS WebKit并不完全开源,只公开了部分的WebCore和JavaScriptCore.有时需要知道iOS Safari的 ...

  9. JXL 读取 Excel java中jxl导出数据到excel的例子 上传文件

    2010-10-14 19:17:06 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Entferne Dat ...

  10. 网站教学 提纲总结到ajax结束后面还有

    Repeater - 重复器五个模板:HeaderTemplate - 在最上面,显示一次FooterTemplate - 最下面,显示一次ItemTemplate - 在中间,显示n次Alterna ...