PKU 1160 Post Office Quad inequality optimization Classic DP

Source: Internet
Author: User

PKU 1160 Post Office Quad inequality optimization Classic DP Post Office The classical dynamic programming problem is mainly embodied in the design of the state and the optimization of the quadrilateral inequalities . Test instructions is: give you n villages, then let you cover these villages with M-post offices, and then let you design the cover way for each village to its corresponding post office distance and shortest The design of the state of the subject is inspired by the "overlay" of the point, the best sub-structure is actually covered with M post office, and with M-1 a post office cover So, the state is dp[n][m] for the first n villages with M-post offices to cover so that each village to its corresponding post office the shortest distance and transfer equation: dp[n][m]=min{dp[k][m-1]+w[k+1,n]} k>=m-1&&k<=n-1 boundary Condition: dp[i][1]=w[1,i] i>=1&&i<=n meaning Dp[n][m] This state can be covered by the first K villages with M-1 Post offices, k+1 to n the shortest distances and transfers obtained by the villages with the first m post Office W[k+1,n] is a decision variable that represents k+1 to n the shortest distance required to establish a post office in these villages by yy can know, n villages if only with a post office to cover, then the post office built in the middle of the village can make the total distance, that is X=[N/2] (square brackets to the rounding) so W[k+1,n] is actually traversing the village to x=[(K+1+n)/2] This point of the distance, summed up the line Analyze an algorithm complexity, the total number of states is O (n*m), state transfer requires O (n), then the total time complexity is O (n*n*m) This problem can be further optimized with quadrilateral inequalities.   because in the dynamic planning, there are such a kind of problems state transition Equation Dp[i][j]=min{dp[i][k-1]+dp[k][j]}+w[i][j] k>i&&k<=j time complexity O (n*n*n) And there are some definitions and theorems as follows: if a function w[i][j], satisfies w[i][j]+w[i '][j ']<=w[i][j ']+w[i '][j] i<=i ' <=j<=j ' is called w satisfies convex quadrilateral inequalities if a function w[i][j], satisfies w[i '][j]<=w[i][j '] i<=i ' <=j<=j ' is called W about the interval containing the monotony of the relationship   theorem 1: If w satisfies both the quadrilateral inequalities and the interval monotone, the DP also satisfies the quadrilateral inequalities Theorem 2: If the theorem 1 satisfies the condition to let Dp[i][j] take the minimum value of K is K[i][j], then k[i][j-1]<=k[i][j]<=k[i+1][j] Note: Theorem 2 is the key to optimization of quadrilateral inequalities, it shows that the decision-making is monotonic, then we can reduce the interval of the decision enumeration and optimize it accordingly. theorem 3:w as convex and only if w[i][j]+w[i+1][j+1]<=w[i+1][j]+w[i][j+1]   A few notes: 1: The proof of theorem 1 is more irritable, in detail can see "optimization techniques of dynamic programming algorithm" Mozie's thesis 2: Theorem 3 actually tells us to verify that W is a convex method, which is to fix a variable and then treat it as a unary function to determine the monotonicity. For example, we can fix J to calculate the expression of w[i][j+1]-w[i][j] about I, see if it is about I increment or decrement, if it is decreasing, then w is convex 3: In practice, we often do not need to be irritable proof, and only need to hit the table, and then observe on the line such as w[i][j],dp[i][j] whether to meet the quadrilateral inequalities Ah, w[i][j] is not monotonous ah, the decision function K[i][j] whether the inequality of the Theorem 2 is met Ah, can be played by the table to engage in   so for the post office problem, the transfer equation is dp[n][m]=min{dp[k][m-1]+w[k+1,n]} k>=m-1&&k<=n-1 is obviously similar to the one above, so we consider using quadrilateral inequalities to optimize and because of the use of quadrilateral inequalities, for the state Dp[i][j] there is an implied condition is i<j, so need to reverse the position of the state optimized to Dp[n][m]=min{dp[k][m-1]+w[k+1,n]} K=k[n][m] is an optimal discontinuity, or an optimal decision point, which means the number of villages that can be covered by the former M-1 post office then there are k[n][m-1]<=k<=k[n+1][m] Note: There may be a small doubt when the K range is limited here, and it seems that k[n-1][m]<=k<=k[n][m+1] is also satisfying monotonicity but unfortunately, in such a state of expression, such an interval limit is wrong!!! The reason is to understand the meaning of K here, K=k[n][m] is recorded in the dynamic planning process, corresponding to the state Dp[n][m] one of the most decision, that is to say dp[n][m] This state is the decision by K, and through the above analysis, We can know that the decision-making is monotonic, so some of the above-mentioned decisions can be used to limit the decision-making interval for subsequent state transitions, so there is a clear k[n][m-1]<=k<=k[n+1][m] This formula is set up, meaning the former m-1 A post office can cover the number of villages K, must be greater than equal to the number of villages before the M-2 post Office can be covered, so the left that inequality is established, and for the right that inequality is only based on the monotony of an upper bound, the essence of our quadrilateral inequality optimization is actually on the left that inequality. In fact, it can be analyzed from the angle of dynamic programming calculation, because for such state and state transfer equation, we calculate the order of state according to M from small to large (stage is m), and the essence of quadrilateral inequality optimization is through the monotonicity of decision-making, We make full use of the previously calculated decision information to reduce the scope of the next enumeration decision, it is obvious that k[n][m-1]<=k[n][m] this inequality is clearly necessary, is our optimization process, the successful use of the previous decision-making information embodied. So if we tune the position of the two variables of the state, then the interval limit of k[n-1][m]<=k<=k[n][m+1] is the correct one . Summary: K of the quadrilateral inequality interval limit, according to the specific k corresponding to the meaning of the state, according to the order of the stage calculation, and then use the decision monotonicity to carry out the limits of the interval, first to meet the meaning of the expression, and then the monotony of decision-making, simply meet the decision monotonicity is not necessarily correct
The code is as follows: #include <cstdio> #include <algorithm>using namespace std; #define INF 0x7ffffff#define MAXN 310int dp[ MAXN][MAXN];//DP[I][J] represents the shortest distance of the first I village put J post Office int w[maxn][maxn];//w[i][j] represents the minimum distance of [i,j] int val[maxn];int s[maxn][maxn];//s I    [j] Records the number of villages before the J-1 post Office, int main () {int n,m,i,j;        while (scanf ("%d%d", &n,&m)!=eof) {for (i = 1; I <= n; i + +) scanf ("%d", &val[i]);            for (int i = 1; I <= n; i + +)//There 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 (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;             for (i = 2; I <= m; i + +) {S[n+1][i] = n;//s[][] Upper bound initialization for (j = n; j > i; J--)                {Dp[j][i] = inf; 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]); }}

  

PKU 1160 Post Office Quad inequality optimization Classic DP

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.