LawrenceTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2427 Accepted Submission (s): 1078
Problem Descriptiont. E. Lawrence is a controversial figure during World War I. He is a British officer who served in the Arabian Theater and led a group of Arab nationals in guerilla strikes against T He Ottoman Empire. His primary targets were the railroads. A highly fictionalized version of his exploits is presented in the blockbuster movie, "Lawrence of Arabia".
You is to write a program to help Lawrence the figure of how to the best use of his limited resources. You are some information from British Intelligence. First, the rail line was completely linear---there is no branches, no spurs. Next, British Intelligence have assigned a strategic importance to each depot---an integer from 1 to 100. A Depot is the no use in its own, it's only have value if it's 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 is 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 this Lawrence only have enough resources for one attack. He cannot attack the depots themselves---they is 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 V Alue that he can achieve for that railroad.
Inputthere would be several data sets. Each data set would begin with a line with the integers, N and M. N is the number of depots on the railroad (1≤n≤1000), and M is the number of attacks Lawrence have resources for (0≤m<n). On the next line would be n integers, each from 1 to, indicating the strategic Value of each depot in order. End of input would be marked by a line with n=0 and m=0, which should isn't be processed.
Outputfor each data set, output a single integer, indicating the smallest strategic Value for the railroad that Lawrence C An achieve and his attacks. Output each of the integer in it own line.
Sample Input
4 14 5 1 24 24 5 1 20 0
Sample Output
172
Source2009 multi-university Training Contest 2-host by Tju
Recommendgaojie
Quadrilateral inequalities optimize DP.
and "POJ 1160" almost the same way of thinking.
F[I][J] represents the minimum cost of the first J-Blast:
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