Use W[i][j] to show that there is no cost between I and J for destruction.
There is a good proof that w[i][j] satisfies the conditions of quadrilateral inequalities. if (W[i+1][j]-w[i][j]) is about J's subtraction function, is satisfies the condition. It can be proved that this w[i][j] is not a condition of hiding.
#include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #include <cctype> #include <cstdio> #include <cstdlib> #include <cstring># Include <iomanip> #include <iostream> #include <algorithm>using namespace std;typedef long LL; const int inf = 0x3fffffff;const int Mmax =1010;int w[mmax];int sum[mmax];int f[mmax];int dp[mmax][mmax];int S[mmax][mmax ];int Cost[mmax][mmax];int Main () {int n,m; while (cin>>n>>m && n+m) {w[0]=0; sum[0]=0; f[0]=0; for (int i=1;i<=n;i++) {scanf ("%d", &w[i]); Sum[i]=sum[i-1]+w[i]; F[I]=SUM[I]*W[I]+F[I-1]; } for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) cost[i][j]= (sum[j-1]-sum[i-1]) *sum[j] -(f[j-1]-f[i-1]); dp[0][0]=0; for (int i=1;i<=n;i++) {dp[0][i]=cost[1][i]; s[0][i]=0; } for (int i=1;i<=m;i++) {dp[i][0]=0; S[i][n+1]=n; for (int j=n;j>=1;j--) {dp[i][j]=inf; int l=s[i-1][j],r=s[i][j+1]; for (int k=l;k<=min (J-1,R); k++) {if (Dp[i][j]>dp[i-1][k]+cost[k+1][j]) {DP[I][J]=DP[I-1][K]+COST[K+1][J]; S[i][j]=k; }}}} printf ("%d\n", Dp[m][n]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Optimization of HDU 2829 dp+ quadrilateral Inequalities