Optimization of HDU 2829 dp+ quadrilateral Inequalities

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.