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

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

2009 Multi-University Training
Contest 2 - Host by TJU

题目大意:

有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(动态规划-四边形不等式)

时间: 2024-08-01 18:12:43

HDU 2829 Lawrence(动态规划-四边形不等式)的相关文章

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

题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程很容易想出来,dp[i][j] 表示前 j 个数分成 i 组.但是复杂度是三次方的,肯定会超时,就要对其进行优化. 有两种方式,一种是斜率对其进行优化,是一个很简单的斜率优化 dp[i][j] = min{dp[i-1][k] - w[k] + sum[k]*sum[k] - sum[k]*sum[

HDU 2829 Lawrence

Lawrence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 282964-bit integer IO format: %I64d      Java class name: Main T. E. Lawrence was a controversial figure during World War I. He was a British officer w

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]+cost[k+1][i]+sum[k]*(sum[i]-sum[k]) cost[k+1][i]=cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k]) 代入DP方程 可以得出 y=dp[k][j-1]-cost[1][k]+sum[k]^2 x=sum[k]. 斜率s

【转】斜率优化DP和四边形不等式优化DP整理

当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重循环跑状态 i,一重循环跑 i 的所有子状态)这样的时间复杂度是O(N^2)而 斜率优化或者四边形不等式优化后的DP 可以将时间复杂度缩减到O(N) O(N^2)可以优化到O(N) ,O(N^3)可以优化到O(N^2),依次类推 斜率优化DP和四边形不等式优化DP主要的原理就是利用斜率或者四边形不等式等数学方法 在所有要判断的子状态中迅速做出判断,所以这里的优化其实是省去了枚举

hdu 2829 dp+四边形不等式优化

用w[i][j]表示i到j之间没有边毁掉的费用. 有一种很好证明w[i][j]是否满足四边形不等式的条件. 若(w[i+1][j]-w[i][j])是关于j的减函数,就是满足条件的.可以证明这里的w[i][j]是瞒住条件的. #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #includ

【HDU】2829 Lawrence

http://acm.hdu.edu.cn/showproblem.php?pid=2829 题意:将长度为n的序列分成p+1块,使得$\sum_{每块}\sum_{i<j} a[i]a[j]$最小(n<=1000,1<=a[i]<=100) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream&

hdu 3480 dp 四边形不等式优化

http://acm.hdu.edu.cn/showproblem.php?pid=3480 给出一个数字集合 S,大小为 n,要求把这个集合分成m个子集,每分出一个子集的费用是子集中的 (max-min)^2,求最小费用. 开始的dp转移很容易想到. 首先对集合从小到大排序,dp[i][j] 表示前i个元素被分成j个子集的最小费用.然后枚举最后一个子集. dp[i][j] = min{dp[k-1][j-1] + cost(k, i)}; 这个转移明显是过不去的,n<10000 m<5000

动态规划专题小结:四边形不等式优化

今天第一次学习四边形不等式优化dp,感觉优化效果十分给力,不过数学味道比较浓重,证明比较复杂.因此这里删繁就简,给出关于四边形不等式优化必须要明白的地方,以后直接套用条件即可. 四边形不等式优化条件 在动态规划中,经常遇到形如下式的转台转移方程: m(i,j)=min{m(i,k-1),m(k,j)}+w(i,j)(i≤k≤j)(min也可以改为max) 上述的m(i,j)表示区间[i,j]上的某个最优值.w(i,j)表示在转移时需要额外付出的代价.该方程的时间复杂度为O(N^3). 下面我们通

hdu 3506 Monkey Party 区间dp + 四边形不等式优化

http://acm.hdu.edu.cn/showproblem.php?pid=3506 四边行不等式:http://baike.baidu.com/link?url=lHOFq_58V-Qpz_nTDz7pP9xCeHnd062vNwVT830z4_aQoZxsCcRtac6CLzbPYLNImi5QAjF2k9ydjqdFf7wlh29GJffeyG8rUh-Y1c3xWRi0AKFNKSrtj3ZY7mtdp9n5W7M6BBjoINA-DdplWWEPSK#1 dp[i][j]表示第