A wave of records of dynamic programming learning

Linear DP

Example question*1:

A company has three mobile attendants, initially at locations 1, 2, and 3.
If a location (represented by an integer) has a request, the company must assign an employee to that location. Only one employee can move at a time, and two employees are not allowed in the same position. Moving an employee from p to q costs c(p, q). This function is not necessarily symmetric, but it is guaranteed that c(p, p) = 0
gives n requests, and the positions of the requests are p1~pn. The company must fulfill all requests in order, the goal is to minimize the cost of the company, please help calculate this minimum cost. N <= 1000, the position is an integer from 1 to 200


Divide the stages: Suppose to complete the i-th task, we only need to consider where the current employee is.
By assigning a waiter, we can move the state from completing i - 1 requests to completing i requests
so the phase is "completed" The number of tasks"
In order to calculate the cost of the waiter, we know the starting position of each waiter, the most direct idea is: put the three waiters in the "state"
f[i, x, y, z]
before finishing i requests, the waiters are located at x, y, z respectively, the minimum cost of the company
considers the state transition, there are obviously three kinds, that is to send one of the three waiters to the i + 1th position
f[i+1, pi+1 , y, z] = min(f[i+1, pi+1, y, z], f[i, x, y, z] + c(x,pi+1));
f[i+1, x, pi+1, z] = min(f[i+1, x, pi+1, z], f[i, x, y, z] + c(y,pi+1));
f[i +1, x, y, pi+1] = min(f[i+1, x, y, pi+1], f[i, x, y, z] + c(z,pi+1));
The scale of the analysis algorithm, the number of states is 1000 * 200^3, which cannot be tolerated.
Careful observation can be found that when the i-th request is completed, there must be an employee at position pi, and only the positions x and y of the other two employees in stage i (note that it is not employees x and y) can describe the status. The employees at pi are redundant information for DP.
Therefore, we can use f[i, x, y] to indicate that when the first i requests are completed, one of the employees is located at pi, and the other two are located at x, y. When , the company's minimum cost
f[i+1,x,y] = min(f[i+1,x,y],f[i,x,y] + c(pi,pi+1));
f[i+1,pi,y] = min(f[i+1,pi,y],f[i,x,y] + c(x,pi+1));
f[i+1,x ,pi] = min(f[i+1,x,pi],f[i,x,y] + c(y,pi+1));
set p3 = 0, so the initial value can be set to p[0 ,1,2] = 0, the goal is p[n,?,?]
Inspiration:
1. To solve the linear dp problem, generally want to determine the "stage". If the "stage" is not enough to represent a state, the required additional information can also be used as the dimension of the state
when transferring. If it always transfers from one stage to the next stage (this question i to i+1), it is not necessary Pay attention to the size change of the additional information dimension (the size of x, y, z in this question is uncertain before and after the transfer), because there is already a "stage" guarantee for no after-
effect set of dimensions".
If the DP state consists of multiple dimensions, check whether these dimensions can be derived from each other, cover the entire state space with as few dimensions as possible, and exclude redundant dimensions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324832633&siteId=291194637