Title Link: http://wikioi.com/problem/1048/
Algorithms and Ideas:
Although it is a relatively simple DP, the introduction of dynamic transfer equation to achieve the card, after all, is a rookie AH.
Dp[i][j] = min (Dp[i][k] + dp[k + 1][j] + s[j]-s[i-1]) k belongs to [I, j];
The process boundary of DP is important,
Maintain a sum[i] array in the input phase to represent the first and the previous I items of the stone,
DP[I][J] means to merge the minimum cost of i~j heap stones, traversing the length of the gravel, Len from 2~n,
The possible value of I is 1~n-len+1,j nature equals i+len-1,
Define k as any heap of stones in I~j, Traverse K to find the minimum cost is dp[i][j];
#include <stdio.h>
#include <string.h>
#define INF 0xffffff
int main ()
{
int n, num[ 111], sum[111] = {0}, min;
int dp[111][111];
memset (DP, 0, sizeof (DP));
scanf ("%d", &n);
for (int i = 1; I <= n; i++)
{
scanf ("%d", &num[i]);
Sum[i] = Sum[i-1] + num[i];
}
for (int len = 2, Len <= N; len++)
{for
(int i = 1; I <= N-len + 1; i++)
{
Int j = i + len-1;
min = INF;
for (int k = i; k < J; k++)
{
if (Dp[i][k] + dp[k + 1][j] + sum[j]-sum[i-1] < min)
min = dp[i][k] + Dp[k + 1][j] + sum[j]-sum[i-1];
}
Dp[i][j] = min;
}
}
printf ("%d\n", Dp[1][n]);
return 0;
}