About the minimum cost sub-distylium

Source: Internet
Author: User

First attempt to write dynamic programming (planning) =

The problem is as follows:

--------------------------------------------------------------------------------------------------------------- ----------

Minimum cost sub-distylium

There are a row of N, for example: 22 14 7 13 26 15 11. Any 2 contiguous number can be merged, the cost of the merger is the sum of the two numbers, after constant merging, and finally into a pile, and all of the cost and called the total cost, give a merging algorithm, the total cost is minimal.
The input and output data formats are the same as the "stone merge".
Input Sample:
4
12 5 16 4

--------------------------------------------------------------------------------------------------------------- ----------

To illustrate the process of the algorithm, we first analyze some simple cases:

(1) n=2:

When n=2, there are only 1 heap methods, so the total merger cost is 2 heap of sand.

(2) N=3:

When n=3, there are 2 kinds of heap methods.

The total cost of the 1th heap method is 20+24, and the total cost of the 2nd method is 11+24. Thus, the final merger cost for the sum of the total amount of sand, and for any merge scheme is the same, so the total cost will depend on the first merger, the 1th method of the 1th merger cost is 20, 2nd method 1th Merge Cost is 11, so the first method is better than 1th.

(3) N=4:

Method A total Cost: 20+34+40=94 method B Total Cost: 21+34+40=95 method C Total Cost: 20+20+40=80 method D Total Cost: 21+27+40=88

Method e Total Cost: 20+27+40=87

When n=4, there are 5 kinds of merging methods, these 5 methods can be divided into 3 categories:

1) including A and b the basic method is to merge the previous 3 heap, in the final heap merge, because the merging of the last heap of the method is the same, so in the first 3 heap merging, different methods will produce different results. The total cost of a is 55 for the total cost of 54,b, so A is better than B in the 1th class method;

2) There are only 1 methods, C, the cost of merging 2 piles respectively is 20, 20, add 40, total 80;

3) including D and E, the basic method is to merge the back 3 piles, and then merge the 1th heap, because the merging of the 1th heap method is the same, so after merging the 3 heap different methods will produce different results, from the above analysis of e than D excellent;

From this we can find that each layer of the optimal solution to find out, you can use a combination of methods to enumerate each of the two solutions to get the solution, because each node is the optimal solution, so the solution of the combination of the optimal solution also must be one of the optimal solution, and then find the smallest one is the root node corresponding to the minimum of the sand pile

Represented by a two-dimensional array:

I just started making a lot of mistakes,

For example, the first thing to do is to write (a blind face):

1  for (int i=1; i<=n;i++) 2          for (int j=i+1; j<=n;j++) 3               for (int k=i;k<=j-1; k++) 4                f[i][j]= (f[i][j]>f[i][k]+f[k+1][j]+g[i][j]) f[i][k]+f[k+1][j]+g[i][j]:f[i][j]; // recursive relationship not considered

Hand waving. jpg

The positive solution is as follows:

1#include"iostream"2#include"Cstdio"3#include"Queue"4 #defineQwq 210000005 intn,m,q;6 using namespacestd;7 intf[ +][ +];8 intg[ +][ +];9 intgra[ +],minn=-2100000;Ten intMain () One { A     intN; -Cin>>N; -      for(intI=1; i<=n;i++) theCin>>Gra[i]; -          -      for(intI=1; i<=n;i++) -g[i][i]=Gra[i];  +           -      for(intI=1; i<=n;i++) +          for(intj=i+1; j<=n;j++) Ag[i][j]=g[i][j-1]+Gra[j]; at              -      for(intI=1; i<=n;i++) -          for(intj=1; j<=n;j++) -f[i][j]= (i!=j)? QWQ:0;//the cost of J (i), I, is +∞ (0) -              -      for(intp=2;p <=n;p++)//heap number of poor lifting//because it is recursive (recursion), so the number of heaps to a large number of in       for(intI=1; I<= (n-p+1); i++)//a period of certainty (Certainty) lists the starting point -      { to          intj=i+p-1;//end Point (Destination) +           for(intk=i;k<=j-1; k++)//The poor lift separates the position//note overflow caused by overrun (Overflow) -F[i][j]= (f[i][j]>f[i][k]+f[k+1][J]+G[I][J])? f[i][k]+f[k+1][j]+G[i][j]:f[i][j]; //Three mesh operator not clear can Baidu, below also has the introduction  the}//correctly write the state transition equation//No effect (unfollow-up Effect) *cout<<f[1][n]<<Endl; $}

Dynamic planning is really a magical thing,

It is suitable for solving the optimal solution of multi-stage (state transition) decision problem, and it can also be used to solve the problem of the optimal solution with linear or nonlinear recurrence relation.

But in fact it's not all-powerful,

1, optimization principle

2, no effect

These two conditions must be met before they can be used,

Of course

Writing dynamic programming is the main thing is to write the state transfer equation,

The next is the boundary condition.

Attach one:

The common use of the trinocular operator is to reduce the usage of the IF statement

For example:

1 i= (whether the brackets are valid or not)? (established): (not established);

About the minimum cost sub-distylium

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.