Lawrence - HDU 2829斜率优化,四边形不等式优化

来源:互联网 发布:sublime的python插件 编辑:程序博客网 时间:2024/05/24 07:01

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2530    Accepted Submission(s): 1125


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 14 5 1 24 24 5 1 20 0
 

Sample Output
172
 


题意:有n个数字,你可以通过轰炸铁路,把他们分成m+1段,每段的权值是其中每两个数字的乘积的和,求所有段的权值和的最小值。

思路:我们设dp[i][j]表示前j个数字轰炸了i次,且第j个数字后面的那段路也是炸掉的情况下的最小权值。

我们来看一下转移,假设dp[i][j]由dp[i-1][k1]和dp[i-1][k2],k1<k2转移过来,我们如何判断该选择哪个。

如果我们要选择后者的话,

dp[i-1][k1]+w[k1+1][j]>=dp[i-1][k2]+w[k2+1][j]

w[k1+1][j]-w[k2+1][j]=(sum[k2]-sum[k1])*(sum[j]-sum[k2])+w[k1+1][k2]

所以(sum[k2]-sum[k1])*sum[j]>=dp[i-1][k2]-dp[i-1][k1]+(sum[k2]-sum[k1])*sum[k2]-w[k1+1][k2]

斜率出来后,我们就可以用斜率优化了。

斜率优化代码如下:

#include<cstdio>#include<cstring>using namespace std;typedef long long ll;int T,t,n,m;ll num[1010],sum[1010],cost[1010][1010],dp[1010][1010];int q[1010],head,tail;void DP(){    int i,j,k;    ll p1,p2,y,y1,y2;    for(i=1;i<=n;i++)       dp[1][i]=cost[1][i];    for(i=2;i<=m+1;i++)    {        head=tail=0;        q[0]=i-1;        for(j=i;j<=n;j++)        {            while(head<tail)            {                p1=q[head];                p2=q[head+1];                y=dp[i-1][p2]-dp[i-1][p1]-cost[p1+1][p2]+sum[p2]*(sum[p2]-sum[p1]);                if(y<=sum[j]*(sum[p2]-sum[p1]))                  head++;                else                  break;            }            k=q[head];            dp[i][j]=dp[i-1][k]+cost[k+1][j];            while(head<tail)            {                p1=q[tail-1];                p2=q[tail];                y1=dp[i-1][p2]-dp[i-1][p1]-cost[p1+1][p2]+sum[p2]*(sum[p2]-sum[p1]);                y2=dp[i-1][j]-dp[i-1][p2]-cost[p2+1][j]+sum[j]*(sum[j]-sum[p2]);                if(y1*(sum[j]-sum[p2])>=y2*(sum[p2]-sum[p1]))                  tail--;                else                  break;            }            q[++tail]=j;        }    }}int main(){    int i,j,k;    while(~scanf("%d%d",&n,&m) && n+m>0)    {        for(i=1;i<=n;i++)        {            scanf("%I64d",&num[i]);            sum[i]=sum[i-1]+num[i];        }        for(k=1;k<=n;k++)           for(i=1;i+k<=n;i++)           {               j=i+k;               cost[i][j]=cost[i][j-1]+num[j]*(sum[j-1]-sum[i-1]);           }        DP();        printf("%I64d\n",dp[m+1][n]);    }}

另外此题还可以用四边形不等式优化。

四边形不等式代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;int T,t,n,m,s[1010][1010];ll dp[1010][1010],w[1010][1010],sum[1010],num[1010],INF=1e18;int main(){    int i,j,k;    ll ret;    while(~scanf("%d%d",&n,&m) && n+m>0)    {        m++;        for(i=1;i<=n;i++)        {            scanf("%I64d",&num[i]);            sum[i]=sum[i-1]+num[i];        }        for(i=1;i<=n;i++)           for(j=i+1;j<=n;j++)              w[i][j]=w[i][j-1]+(sum[j-1]-sum[i-1])*num[j];        for(i=1;i<=m;i++)           for(j=i+1;j<=n;j++)              dp[i][j]=INF;        for(i=1;i<=n;i++)           dp[1][i]=w[1][i];        for(i=2;i<=m;i++)        {            s[i][n+1]=n;            for(j=n;j>i;j--)            {                for(k=s[i-1][j];k<=s[i][j+1];k++)                {                    ret=dp[i-1][k]+w[k+1][j];                    if(ret<dp[i][j])                    {                        dp[i][j]=ret;                        s[i][j]=k;                    }                }            }        }        printf("%I64d\n",dp[m][n]);    }}





0 0
原创粉丝点击