Wiki OI 1048 Stone Merge

Source: Internet
Author: User

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;
}


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.