SPOJ 345 Mixtures

Source: Internet
Author: User

Question Portal: Enter

Analysis:

This question is the deformation of "Stone merge. Dynamic Planning is also used for solution.

Use dp [I, j] to represent the minimum amount of smoke released by the j-stack mixture merger starting from heap I

Sum [I, k] indicates the color of the combined j heap mixture starting from heap I.

Obviously there is a state transition equation:

Dp [I, j] = min {dp [I, k] + dp [I + k, j-k] + sum [I, k] * sum [I + k, j-k]} (1 <= k <j)

It is not difficult to write the code:

01 # include <iostream>
02 # include <cstring>
03 # include <climits>
04 using namespace std;
05
06 int main (int argc, char * argv [])
07 {
08 ios: sync_with_stdio (false );
09 int I, j, k, m, temp;
10 int n;
11 int mix [101];
12 int dp [1, 101] [2, 101];
13 int sum [101] [101];
14
15 while (cin> n)
16 {
17 memset (mix, 0, sizeof (mix ));
18 memset (dp, 0, sizeof (dp ));
19 memset (sum, 0, sizeof (sum ));
20 for (I = 1; I <= n; ++ I)
21 {
22 cin> mix [I];
23 sum [I] [1] = mix [I];
24 dp [I] [1] = 0;
25}
26 for (I = 1; I <= n; ++ I)
27 {
28 for (j = 2; I + j <= n + 1; ++ j)
29 {
30 sum [I] [j] = (sum [I] [j-1] + mix [I + j-1]) % 100;
31}
32}
33
34 for (I = n; I> = 1; -- I)
35 {
36 for (j = 2; I + j <= n + 1; ++ j)
37 {
38 m = INT_MAX;
39 for (k = 1; k <j; ++ k)
40 {
41 temp = dp [I] [k] + dp [I + k] [j-k] + sum [I] [k] * sum [I + k] [j- k];
42 if (temp <m)
43 m = temp;
44}
45 dp [I] [j] = m;
46}
47}
48 cout <dp [1] [n] <endl;
49}
50 return 0;
51}

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.