• 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
     


    题目大意:

    有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;
    }
    






  • 相关阅读:
    20121010 闲的慌的日子
    九月,桂花飘香的季节
    win7下控件(ActiveX)注册错误(0x80040200) 修正
    C#多线程技术(一)
    C#多线程技术(二)
    C++沉思录摘录(OOP部分)
    SVM入门教程
    是IE的bug还是Windows的bug?
    inno setup安装时不需要开始菜单项
    django本地局域网访问
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4261964.html
Copyright © 2020-2023  润新知