Dynamic Planning (dp) Algorithm

Source: Internet
Author: User

Dynamic Programming is a way and method to solve the optimization problem, rather than a special algorithm. Unlike search or numeric calculation, it has a standard mathematical expression and a clear and clear solution. Dynamic Programming is usually aimed at an optimization problem. Due to the different nature of various problems, the conditions for determining the optimal solution are also different. Therefore, the design method of dynamic planning is different for different problems, there are different solutions, but there is no universal dynamic planning algorithm, can solve all kinds of optimization problems. Therefore, in addition to a correct understanding of the basic concepts and methods, readers must analyze and handle specific problems, build models with rich imagination, and use creative techniques to solve them. We can also analyze and discuss Dynamic Planning Algorithms for several representative problems, and gradually learn and master this design method.

Therefore, the difficulty of the dp algorithm lies in finding the state transition equation. I have made some dynamic planning questions in poj, And I will summarize them here.


(1) poj 1160

Question:

Set up p post offices in the v villages (the villages are in a straight line) to minimize the total distance from each village to the post office.

B solution:

Cost [v] [p] represents the shortest distance for building p post offices in v villages

Dis [v] [v]; dis [I] [j] indicates creating a post office between village I and Village j. Obviously, this post office should be built between I and j.

C state transition equation:

Cost [v] [p] = cost [I] [P-1] + dis [I + 1] [v] // p <= I <= v;



2. poj 1260

A Problem description:

There are c Types of jewelry, each type of jewelry has the number of needs to be purchased n and the corresponding value p, need to buy a certain kind of need to spend (n + 10) * p; low-quality jewelry can be replaced by high-quality jewelry at the time of purchase; at the end of the purchase, the minimum cost is required.

B solution:

Dp [c]; dp [I] indicates the minimum amount of money spent on the zhubao in section I.

Num [c]; num [I] indicates the total number of jewels of quality from 0 to I.

C state transition equation:

Dp [I] = (num [I] + 10) * p [I]

Dp [I] = min (dp [I], dp [j] + (num [I]-num [j] + 10) * p [I]) // 0 <= j <I


3. poj 1276

A Problem description:

There are n coins, each of which has ai sheets and the value di. The requirement is to generate m values and find the closest m value.

B solution:

This is a problem with multiple backpacks. You can add up the banknotes with repeated values or directly treat them as one individual. In this way, it becomes the 01 backpack.

C state transition equation:

For (I = 1; I <= count; I ++) // count the number of coins, v [I] corresponds to the value of the I coins

For (k = m; k> = v [I]; k --) // m is the final value

Opt [k] = opt [k] | opt [k-v [I]; // if k can be spelled out, opt [k] = 1;



4. poj 1745

A Problem Description

The number of columns is given. You can add or subtract between two adjacent numbers to determine whether all the results can be divisible by k.

B Solution

C [10001] [101]; C [I] [j] indicates the sum, sum % k = j of the Operation Result of the I number. If yes, it is 1; otherwise, it is 0.

C state transition equation

C [I] [j] = C [I-1] [(j-a [I]) % k] | c [I-1] [(j + a [I]) % k]; // here we need to prevent j-a [I] and j + a [I] from being negative, so we still need to do some operations. For convenience, we will not write it here.


5. poj 1837

A Problem Description

Balance Problem of the balance. There is a G weight. Each weight is g [I]. You need to attach the n weights to the C scales of the balance (c [I] indicates that the scale is up to standard ), the balance is required. The scale on the left of the balance is represented by a negative number, and the scale on the right is represented by a positive number. There are several balancing methods.

B Solution

Offset is required because a negative number exists. The maximum offset is M = the maximum weight of the weight * maximum scale * the maximum number of weights.

W [I] [j] // Several Methods indicating that the moment is j after the I-th weight is attached

C status transfer

For (I = 1; I <= G; I ++) // G weight

For (j = 1; j <= C; j ++) // C Scale

For (k = 0; k <7501; k ++) // torque

{

W [I] [k + g [I] * c [j] + = w [I-1] [k]; // attaches the I weight to the j scale, and the torque changes.

}

Cout <w [G] [3500] <endl;




Related Article

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.