POJ 1160 Post Office//Quad inequality optimization

Source: Internet
Author: User
Tags min

Http://blog.csdn.net/find_my_dream/archive/2009/12/03/4931222.aspx

This blog is generally better

Optimization of quadrilateral inequalities------post-office problems

M[p,n] for the solution of the problem

M[I,J] represents the minimum distance to establish I post office in the former J Village and

State transition equations and boundary conditions are

M[1,J]=W[1,J]

M[i,j]=min (M[i-1,k]+w[k+1,j]) i<=j

Where W[i,j] is expressed in d[i. J] Establish a minimum distance between the post office and,

It can be proved that when only one post office is established, the optimal solution appears in the median,

The village where the post office is set is K, then k= (i+j)/2

Calculation W[i,j] is relatively simple, do not elaborate

At the same time make s[i,j]=k, record the number of villages used before i-1 a post office

It is easy to construct the optimal scheme after calculating the minimum distance and structure.

The above algorithm w[i,j] can be processed by O (n) time, in the time of O (1) calculated,

Therefore, the total state of the algorithm is O (n*p), and the number of States transferred per state is O (n),

The time for each state transfer is O (1), and the total time complexity of the algorithm is O (p*n^2).

The following is the optimization of the algorithm

First the state transfer equation of this topic let me guess whether it also satisfies monotonicity

IE S[i-1,j]<=s[i,j]<=s[i,j+1]

First we prove that the function W satisfies the quadrilateral inequalities, i.e.

W[i,j]+w[i ', J ']<=w[i ', J]+w[i,j '], i<=i ' <=j<=j '

Set y= (i ' +j)/2 z= (I+J ')/2

The following can be discussed in two situations, z<=y and z>y, but these two situations are similar

Proof can refer to Maoziqing Daniel's paper, not elaborated

The mathematical induction method is used to prove that the M function also satisfies the quadrilateral inequalities.

Finally, we prove that decision S[i,j] satisfies monotonicity.

For the convenience of discussion, make mk[i,j]=m[i-1,k]+w[k+1,j];

Let's first prove s[i-1,j]≤s[i,j],

As long as proven for all i≤k<k ' <j and Mk ' [I-1,j]≤mk[i-1,j],

There: Mk ' [I,j]≤mk[i,j].

Similarly, we can prove a stronger inequality

MK[I-1,J]-MK ' [i-1,j]≤mk[i,j]-mk ' [I,j]

That is: Mk[i-1,j]+mk ' [i,j]≤mk[i,j]+mk ' [I-1,j]

Using the recursive definition to expand the collation: M[i-2,k]+m[i-1,k ']≤m[i-1,k]+m[i-2,k '],

This is the quadrilateral inequality of M in I-2<i-1<k<k '.

Let's prove s[i,j]≤s[i,j+1],

Similar to the above, set K<k ' <j, we only need to prove a stronger inequality:

MK[I,J]-MK ' [i,j]≤mk[i,j+1]-mk ' [i,j+1]

That is: Mk[i,j]+mk ' [i,j+1]≤mk[i,j+1]+mk ' [I,j]

Using the recursive definition to expand the collation:

W[k+1,j]+w[k ' +1,j+1]≤w[k+1,j+1]+w[k ' +1,j],

This is the quadrilateral inequality of W when K+1<k ' +1<j<j+1.

In summary, the problem of decision-making s[i,j] is monotonic,

M[1,J]=W[1,J]

M[i,j]=min (M[i-1,k]+w[k+1,j]) i<=j

S[i,j]=k

As mentioned above, the time complexity of the optimized algorithm is O (n*p).

#include <cstdio> #define INF 0x7ffffff int DP[301][31];//DP[I][J] represents the shortest distance from the first I village to J post Offices int w[301][301];//w[i][j] means [ I,J] the minimum distance int val[301]; int S[301][31];//s[i][j] J-1 number of villages before the first post Office int main () {int n,m; while (scanf ("%d%d", &n,&m)!=eof) {for (int i=1;i<= n;i++) scanf ("%d", &val[i]); for (int i=1; i<=n; i++)//Here is a recursive formula that can be preprocessed {w[i][i]=0; for (int j=i+1;j<=n;j++) w[i][j]=w[i][j-1]+val[j]-val[(j + I )/2]; } for (int i=1;i<=n;i++) {dp[i][1]=w[1][i]; s[i][1]=0;}//for (int i=1;i<=m;i++) s[n+1][i]=n; The for (int i=2;i<=m;i++) {s[n+1][i]=n;//s[][] upper bound initialization for (int j=n;j>i;j--)//Decision amount S[I][J] has been shortened by the scope of the search {dp[j][i]=inf;/* Enumerate k values in the range of s[i-1][j] to s[i][j+1], calculate the distance between the first K villages and the establishment of a I-1 post office, the K+1 village, and the first J village to establish a post office. If the distance is the smallest, write down the scheme./For (int k=s[j][i-1];k <=s[j+1][i];k++) {int tmp=dp[k][i-1]+w[k+1][j], if (Tmp<dp[j][i]) {dp[j][i]=tmp; s[j][i]=k;}}} } printf ("%d/n", Dp[n][m]); } return 0; }

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.