【HDU 2829】Lawrence

LawrenceTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2427Accepted Submission(s): 1078

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

Source

2009 Multi-University Training Contest 2 – Host by TJU

Recommend

gaojie

四边形不等式优化dp。

和【POJ 1160】几乎一样的思路。

f[i][j]表示前j个炸i次的最小代价:

f[i][j]=min(f[i-1][k]+w[k+1][j])

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#define LL long longusing namespace std;int n,m;LL w[1005][1005],p[1005][1005],f[1005][1005],a[1005];int s[1005][1005];void Getw(){for (int i=2;i<=n;i++)for (int j=1;j<i;j++)p[i][j]=p[i][j-1]+a[i]*a[j];for (int i=1;i<n;i++)for (int j=i+1;j<=n;j++)w[i][j]=w[i][j-1]+p[j][j-1]-p[j][i-1];}int main(){while (scanf("%d%d",&n,&m)!=EOF&&n){m++;for (int i=1;i<=n;i++)scanf("%lld",&a[i]);Getw();memset(f,127,sizeof(f));for (int i=1;i<=n;i++){f[1][i]=w[1][i];s[1][i]=0;}for (int i=2;i<=m;i++){s[i][n+1]=n;for (int j=n;j>=i;j–)for (int k=s[i-1][j];k<=s[i][j+1];k++)if (f[i-1][k]+w[k+1][j]<f[i][j])f[i][j]=f[i-1][k]+w[k+1][j],s[i][j]=k;}printf("%lld\n",f[m][n]);}return 0;}

,含泪播种的人一定能含笑收获。

【HDU 2829】Lawrence

相关文章:

你感兴趣的文章:

标签云: