The third chapter summarizes the work

First, understand the dynamic programming algorithm

   Dynamic programming algorithm is a decision problem from one respect. For example, Xiao Ming from a starting point to the destination d, midway might go through b1, b2, b3, c1, c2, c3, and through different cost of these places, Xiao Ming how to go to make the most expensive?

   The minimum cost from the perspective of dynamic programming, if Bob can know the minimum cost (cost [b] [d]) from b to d plus the cost ([a] [b]), you can know that he reached the finish line d the optimal solution. To know the cost [b] [d], must be known min (cost [c] [d]) ...... such layers recursively, to a big problem into a small problem, and these small problem and rely on each other, there is order, small problem of the optimal solution and a big problem related to the final solution, which is the decision-making in terms of a dynamic programming. Dynamic programming is often used as a table to record a progressive small problem, finally get the answer to the big question of the means of two-dimensional array.

Second, recursive programming problems Equation 1

1. longest monotonically increasing sequence

Suppose A [j] indicates the end of the longest monotonic incremental sequence number j.

Recursively equation A [I] = max (A [J] +. 1, A [I])   (I> =. 1, 0 <= J <I)

 1 int max_length(int n, int a[]){
 2     for(int end = 1; end < n; end++){
 3         for(int start = 0;start < end ; start++){
 4             if(a[end] > a[start] && length[end] < length[start] + 1)    length[end] = length[start] + 1;    
 5         }
 6     }
 7     int max_length = length[0];
 8     for(int i = 0; i < n; i++){
 9         if(max_length < length[i])    max_length = length[i];
10     }
11     return max_length; 
12 }

2. The problem hire a yacht

Suppose cost [i] [j] represents the minimum cost to the site from a site j i.

cost[i][j] = max(cost[i][j], cost[i][k]+cost[k][j])     (1 <= i < n, i < j <= n, i <= k <= j)

 1 void minzj(int b[201][201], int n)
 2 {
 3 
 4     for(int r=2;r<=n;r++)
 5     {
 6         for(int i=1;i<=n-r+1;i++)
 7         {
 8             int j = i+r-1;
 9             for(int k=i+1;k<j;k++)
10             {
11                 int t = b[i][k] + b[k][j];
12                 if(t<b[i][j])
13                 {
14                     b[i][j] = t;
15                 }
16             }
17         }
18     }
19 }

Third, pair programming summary

To learn dynamic programming, a little fuzzy.

 

Guess you like

Origin www.cnblogs.com/lycsuper/p/11787960.html