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
  1. 4 1
  2. 4 5 1 2
  3. 4 2
  4. 4 5 1 2
  5. 0 0
 
Sample Output
  1. 17
  2. 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 满足四边形不等式优化的条件。

解题代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. const int maxn=1100;
  8. ll cost[maxn][maxn],dp[maxn][maxn],a[maxn];
  9. int n,m,s[maxn][maxn];
  10.  
  11. void input(){
  12. for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
  13. for(int i=1;i<=n;i++){
  14. ll sum=0;
  15. cost[i][i]=0;
  16. for(int j=i+1;j<=n;j++){
  17. sum+=a[j-1];
  18. cost[i][j]=cost[i][j-1]+sum*a[j];
  19. }
  20. }
  21. for(int i=0;i<=n;i++){
  22. dp[0][i]=cost[1][i];
  23. s[0][i]=0;
  24. s[i][n+1]=n;
  25. }
  26. }
  27.  
  28. ll solve(){
  29. for(int i=1;i<=m;i++){
  30. for(int j=n;j>=1;j--){
  31. dp[i][j]=1e18;
  32. for(int k=s[i-1][j];k<=s[i][j+1];k++){
  33. if(dp[i-1][k]+cost[k+1][j]<dp[i][j]){
  34. dp[i][j]=dp[i-1][k]+cost[k+1][j];
  35. s[i][j]=k;
  36. }
  37. }
  38. }
  39. }
  40. cout<<dp[m][n]<<endl;
  41. }
  42.  
  43. int main(){
  44. while(scanf("%d%d",&n,&m)!=EOF && (m||n) ){
  45. input();
  46. solve();
  47. }
  48. return 0;
  49. }

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. javascript 判断IOS版本号

    先来观察 iOS 的 User-Agent 串: iPhone 4.3.2 系统: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; ...

  2. Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边

    Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边,当本地有图片的时候,直接从本地读取图片,如果本地没有图片,将从服务器异步加载图片 package com.example. ...

  3. Cocos2dx 3.0 过渡篇(三十一)ValueVector和Vector不得不说的故事

    本文投票地址:http://vote.blog.csdn.net/Article/Details?articleid=37834689 前天看到一个颇为纠结的选择题:有一天你遇到一个外星人,这时外星人 ...

  4. View实现涂鸦、撤销以及重做功能

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  5. C#用正则表达式去掉Html中的script脚本和html标签

    原文 C#用正则表达式去掉Html中的script脚本和html标签 /// <summary>         /// 用正则表达式去掉Html中的script脚本和html标签     ...

  6. 将单词首字母大写的JS脚本工具

    <html> <head> <title>首字母全改为大写JS脚</title> <SCRIPT LANGUAGE="JavaScrip ...

  7. android中Logcat的深层理解

    Android的开发也能够归类为嵌入式设备的开发.即便不是嵌入式开发,依旧要注意对内存和处理的使用.养成一个好的习惯对自己的帮助是非常大的. 在Log的源代码中能够看到这种凝视: The order ...

  8. 简单的RPC java实现

    RPC的名声大噪之时是在2003年,那一个“冲击波”病毒(Blaster Worm virus)袭卷全球的一年.而“冲击波”正是用着RPC这把刀来敲开了远程电脑的大门.当然RPC 有更多正面的应用,比 ...

  9. Cmpletepack coming~^.^

    昨天小小总结了01背包:01背包 不足之处还望多提意见~噶呜~ 今天来总结一下完全背包: 完全背包:    基本思路:类似于01背包,所不同的是每种物品有无限件.也就是从每种物品的角度考虑,策略已经不 ...

  10. Java DatagramSocket(UDP)要注意的问题

    因为byte数组的大小问题,在网络发送过程中.可能包括多余的空格,若接收时要对数据进行比較,最好先将数据做下处理,处理掉多余的空格.