Stone Merge (DP)

Source: Internet
Author: User

Stone merge Time limit: 1 Sec Memory Limit: MB
Submit: Solved: 22
[Submit] [Status] [Web Board] Description

Now there are n heaps of stones, and the first heap has AI stones. Now to merge these stones into a pile, each can only merge adjacent two, the cost of each merger is the total number of stones of two piles. The minimum cost of merging all stones.

Input

The first line contains an integer T (t<=50) that represents the number of data groups.
The first row of each set of data contains an integer n (2<=n<=100) that represents the heap number of the stone.
The second line contains n positive integer ai (ai<=100), which represents the number of stones per heap.

Output

Only one row per group of data represents the minimum consolidation cost.

Sample Input
241 2 3 453 5 2 1 4
Sample Output
1933
HINT
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define Len 200005#define mod 19999997const int INF = 0x3f3f3f3f; #define EXP 1e-6 int sum[105],dp[105][105],a[105];    int main () {int t,n,i,j,k,l;    scanf ("%d", &t);        W (t--) {scanf ("%d", &n);        MEM (sum,0);        MEM (dp,0);            Up (i,1,n) {scanf ("%d", &a[i]);        Sum[i]=sum[i-1]+a[i];                } up (L,1,n) {up (i,1,n-l) {j = i+l;                Dp[i][j]=inf;                int tem = SUM[J]-SUM[I-1]; Up (K,I,J)               Dp[i][j]=min (Dp[i][j],dp[i][k]+dp[k+1][j]+tem);    }} printf ("%d\n", Dp[1][n]);    }}/************************************************************** problem:1592 user:aking2015 language:c++ result:accepted time:108 Ms memory:1528 kb****************************************************************/







Stone Merge (DP)

Related Article

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.