Title Link: http://acm.swust.edu.cn/problem/code/745255/
Time limit (ms): 65535 Description The following method is called the minimum cost of the letter tree: Given a sequence of positive integers, for example: 4,1,2,3, adding them without changing the position of the number, and using parentheses to Mark each addition to get the sum.
For example: ((4+1) + (2+3)) = ((5) + (5)) = 10. Except the original number is not 4,1,2,3, the rest are intermediate results, such as 5,5,10, add the intermediate results, get: 5+5+10= 20, then the number 20 is called the price of this sequence, if you get another algorithm: (4+ ((1+2) +3) = (4+ ((3) +3)) = (The 4+ (6)) = 10, the other cost of the sequence is: 3+6+10=19. If the number of n is given, the minimum cost of this sequence can be calculated by adding N-1 to the parentheses.
Note: The result range does not exceed longint. Input first acts n (1≤n≤200), the second behaves n positive integers, and the integers are separated by a space. Output outputs only one row, which is the minimum generation value. Sample Input
Sample Output
Hint
Problem -Solving ideas: A DP problem DP[I][J] represents the minimum cost of interval [i,j], so in dp[i][j] Find a K value divided into two parts to try different combinations of numbers, to find the minimum cost in [I,j]
a DP equation was then obtained as follows Dp[i][j]=min (Dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);The code is as follows:
1#include <iostream>2#include <cstring>3 using namespacestd;4 #defineMin (A, b) a<b?a:b5 #defineINF 0x3f3f3f3f6 7 intsum[201], dp[201][201];//Dp[i][j] Indicates the minimum cost of the interval [i,j]8 intMain ()9 {Ten intN, I, J, K, R, X; OneCIN >>N; AMemset (DP, INF,sizeof(DP)); - for(i =1; I <= N; i++){ -CIN >>x; theSum[i] = sum[i-1] +x; -Dp[i][i] =0; - } - for(R =0; R < N; r++){ + for(i =1; I <= n-r; i++){ -j = i +R; + for(k = i; k < J; k++){ ADp[i][j] = min (Dp[i][j], dp[i][k] + dp[k +1][J] + sum[j]-sum[i-1]); at } - } - } -cout << dp[1][n] <<Endl;; - return 0; -}View Code
Using a similar DP method can be solved [swust OJ 574]rentboat [swust OJ 419] Bracket pairing [Nyoj 37] palindrome string
[Swust OJ 404]--Minimum cost tree (dynamic planning)