The main problem: there are n enemy arsenals in a straight line, each arsenal has a value VI, and any adjacent two libraries are connected to each other. For any one linked Arsenal chain, its threat to us can be represented by the function W (i,j) as: W (i,j) =vi*sum (i+1,j) +w (i+1,j) i<j;
W (i,j) =0 i=j;
Now, you have a M bomb, each of which can blow up the channel between adjacent two libraries, seeking the ultimate total minimum threat value.
Title Analysis: the definition State DP (I,J) means that the first J storeroom is removed from the chain by the I bomb to the minimum threat value generated by the former J storeroom, then the state transition equation is: DP (I,J) =min (DP (i-1,k-1) +w (k,j)). It is obvious that W (i,j) satisfies convex quadrilateral inequalities and is monotonous in relation to inclusions, so DP (I,J) also satisfies convex quadrilateral inequalities, which can limit the range of K to reduce the processing of state and achieve the optimization effect.
PS: Maybe my code is too bad! Run, 400+ms!!!. No one else is using 100ms!.
The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;# define LL long longconst ll Inf=0xfffffffffffffff;const int N=1005;int n,m; LL Dp[n][n];int K[n][n]; LL w[n][n];int s[n],a[n];void init () {s[0]=0; for (int i=1;i<=n;++i) {scanf ("%lld", a+i); S[I]=A[I]+S[I-1]; } for (int j=n;j>=1;--j) {for (int i=1;i<=n;++i) Dp[i][j]=inf; w[j][j]=0; for (int i=j-1;i>=1;--i) w[i][j]=a[i]* (S[j]-s[i]) +w[i+1][j]; }}void Solve () {if (m==0) {printf ("%lld\n", W[1][n]); return; } for (int i=0;i<n;++i) {dp[0][i]=inf; dp[i][i]=0; K[i][i]=i; } for (int l=2;l<=n;++l) {for (int i=1;i+l-1<=n;++i) {int j=i+l-1; Dp[i][j]=inf; for (int k=k[i][j-1];k<=k[i+1][j];++k) {if (Dp[i][j]>dp[i-1][k-1]+w[k][j]) {Dp[i][j] =DP[I-1][K-1]+W[K][J]; K[i][j]=k; }}}} LL Ans=inf; for (int i=1;i<n;++i) ans=min (Ans,dp[m][i]+w[i+1][n]); printf ("%lld\n", ans);} int main () {while (scanf ("%d%d", &n,&m) && (n+m)) {init (); Solve (); } return 0;}
HDU-2829 Lawrence (optimization of dp+ quadrilateral inequalities)