Interval dynamic programming of gravel merging---

Source: Internet
Author: User

Title Description Description

There are n heap of stones in a row, each pile of stones have a weight of w[i], each merge can merge adjacent two piles of stones, the cost of a merger is the weight of two piles of stone and w[i]+w[i+1]. Ask what sort of merger sequence it takes to minimize the total merger cost.

Enter a description input Description

First line an integer n (n<=100)

Second row n integers w1,w2...wn (wi <= 100)

outputs description output Description

An integer representing the minimum consolidation cost

sample input to sample

4

4 1 1 4

Sample output Sample outputs

18

data size & Hint the solution of the subject is seen in the Codevs of the puzzle, and now for reference, it feels very good. for the problem of interval DP, we can use the form of memory search, or we can take the form of recursion. But the essence of the two is the same. we can transform the problem of the merging of the stones into the dividing of the interval and the problem, and establish the mathematical model. The problem of stone merging has been transformed into the least cost of interval division. Note that the point is in the memory of the search to write the appropriate initialization, and pay attention to interval division do not appear infinite recursion, choose the dividing point. The recursive form of writing is equivalent to the length of the enumeration interval, and then the dividing point of the interval is enumerated. The two methods are essentially the same: I hope both of these methods can be learned and mastered skillfully.
Recursive method:
#include <stdio.h> #include <algorithm>using namespace Std;int a[105];int sum[105];int d[105][105];const int INF = (1 <<), int main () {int n, i;scanf ("%d", &n), for (i = 1; I <= n; i++) {scanf ("%d", &a[i]); sum[i ] = Sum[i-1] + a[i];} int Len, J, k;for (len = 1; len < n; len++) {for (i = 1; I <= N-len; i++) {int res = INF;J = i + len;for (k = i; K & Lt J k++) res = min (res, d[i][k] + d[k + 1][j] + sum[j]-sum[i-1]);d [i][j] = res;}} printf ("%d\n", D[1][n]); return 0;}

Recursion is the enumeration of all possible occurrences, and then a linear scan to find the least cost.
The following is a mnemonic search, which is easier to understand than recursion
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int a[110];int sum[110];int dp[110][110];const int INF = (1 <<); int solve (int l, int r) {if (Dp[l][r]! =-1) re Turn Dp[l][r];int res = inf;for (int i = l; I <= r-1; i++) res = min (res, solve (l, i) + solve (i + 1, R) + sum[r]-sum [L-1]); return Dp[l][r] = res;} int main () {int n;scanf ("%d", &n), for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]), sum[i] = Sum[i-1] + a[i];} Memset (DP,-1, sizeof (DP)), for (int i = 1; I <= n; i++)    //Note to initialize to 0dp[i][i] = 0;printf ("%d\n", Solve (1, n)); return 0 ;}


Interval dynamic programming of gravel merging---

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.