四边形不等式优化DP

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2220    Accepted Submission(s): 975

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
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; const int maxn=1100; int n,m;
LL a[maxn],sum[maxn];
LL dp[2][maxn],cost[maxn][maxn];
int s[2][maxn]; int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=1;i<=n;i++)
{
scanf("%I64d",a+i);
sum[i]=sum[i-1]+a[i];
}
memset(cost,0,sizeof(cost));
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
cost[i][j]=cost[i][j-1]+(sum[j-1]-sum[i-1])*a[j];
memset(dp,63,sizeof(dp)); for (int i = 1; i <= n; ++i)
{
dp[0][i]=cost[1][i];
s[0][i]=1;
}
int now=1,pre=0; for(int j=1;j<=m;j++)
{
s[now][n+1]=n-1;
for(int i=n;i>=j;i--)
{
for(int k=s[pre][i];k<=s[now][i+1];k++)
{
int temp=dp[pre][k]+cost[k+1][i];
if(temp<dp[now][i])
{
dp[now][i]=temp;
s[now][i]=k;
}
}
}
swap(now,pre);
} printf("%I64d\n",dp[pre][n]);
}
return 0;
}

HDOJ 2829 Lawrence的更多相关文章

  1. hdoj 2829 Lawrence 四边形不等式优化dp

    dp[i][j]表示前i个,炸j条路,并且最后一个炸在i的后面时,一到i这一段的最小价值. dp[i][j]=min(dp[i][k]+w[k+1][i]) w[i][j]表示i到j这一段的价值. # ...

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

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

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

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

  4. 【HDU】2829 Lawrence

    http://acm.hdu.edu.cn/showproblem.php?pid=2829 题意:将长度为n的序列分成p+1块,使得$\sum_{每块}\sum_{i<j} a[i]a[j]$ ...

  5. HDU 2829 Lawrence(动态规划-四边形不等式)

    Lawrence Problem Description T. E. Lawrence was a controversial figure during World War I. He was a ...

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

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

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

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

  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 设dp[i][j]表示前i点,炸掉j条边的最小值.j<i dp[i][j]=min{dp[k][j-1]+cost[k+1][i]} 又由得出cost[1][i]=cost[1][k] ...

随机推荐

  1. [51nod 1022] 石子归并v2 [dp+四边形不等式优化]

    题面: 传送门 思路: 加强版的石子归并,现在朴素的区间dp无法解决问题了 首先我们破环成链,复制一条一样的链并粘贴到原来的链后面,变成一个2n长度的序列,在它上面dp,效率O(8n^3) 显然是过不 ...

  2. loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】

    题目链接 loj300 题解 orz litble 膜完题解后,突然有一个简单的想法: 考虑到\(2\)是质数,考虑Lucas定理: \[{n \choose m} = \prod_{i = 1} { ...

  3. bzoj 2435 dfs处理

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...

  4. 【转】手摸手,带你用vue撸后台 系列三(实战篇)

    前言 在前面两篇文章中已经把基础工作环境构建完成,也已经把后台核心的登录和权限完成了,现在手摸手,一起进入实操. Element 去年十月份开始用vue做管理后台的时候毫不犹豫的就选择了Elemen, ...

  5. 解决当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭)的问题

    当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭) 发生这种情况时 打开项目目录中的 Temp文件夹,可以找到 一个 UnityLockfile 文件 将这个文件删除就可 ...

  6. 控制cxGrid 主从表的明细只展开一个

    procedure TForm.ADetailDataControllerCollapsing( ADataController: TcxCustomDataController; ARecordIn ...

  7. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---41

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  8. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---2

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...

  9. 多个电脑之间使用相同的ssh密钥

    首先我们给最先创建的密钥的电脑取名为OLD, 给后创建的密钥的电脑取名为NEW,在OLD上创建密钥,文件默认保存在 ~/.ssh/ 中: ssh-keygen –t rsa –C "your ...

  10. python 代码格式

    python 代码格式 Python对代码的缩进要求非常严格,如果不采用合理的代码缩进,将抛出SyntaxError异常 Python语句中一般以新行作为为语句的结束符.但是我们可以使用斜杠( )将一 ...