poj 1738 An old Stone Game(区间dp 合并石子问题直线型)

来源:互联网 发布:windows update不更新 编辑:程序博客网 时间:2024/05/02 00:59

1、http://poj.org/problem?id=1738

参考百度文库:http://wenku.baidu.com/view/adac87bbfd0a79563c1e726a.html

2、

An old Stone Game
Time Limit: 5000MS Memory Limit: 30000KTotal Submissions: 2278 Accepted: 543

Description

There is an old stone game.At the beginning of the game the player picks n(1<=n<=50000) piles of stones in a line. The goal is to merge the stones in one pile observing the following rules:
At each step of the game,the player can merge two adjoining piles to a new pile.The score is the number of stones in the new pile.
You are to write a program to determine the minimum of the total score.

Input

The input contains several test cases. The first line of each test case contains an integer n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game.
The last test case is followed by one zero.

Output

For each test case output the answer on a single line.You may assume the answer will not exceed 1000000000.

Sample Input

110033 4 341 1 1 10

Sample Output

0178

 3、wrong 代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 50005#define MAX 0x7fffffffint stone[N];int sum[N];int dp[N][N];int main(){    int n,add;    while(scanf("%d",&n)!=EOF)    {        if(n==0)        break;        for(int i=0; i<n; i++)            scanf("%d",&stone[i]);            sum[0]=stone[0];            for(int i=1;i<n;i++)            {                sum[i]=sum[i-1]+stone[i];            }            for(int i=0;i<n;i++)            dp[i][i]=0;            for(int v=1;v<n;v++)            {                for(int i=0;i<n-v;i++)                {                    int j=i+v;                    dp[i][j]=MAX;                    if(i>0)                    add=sum[j]-sum[i-1];                    else                    add=sum[j];                    for(int k=i;k<j;k++)                    {                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+add);                    }                }            }            printf("%d\n",dp[0][n-1]);    }    return 0;}


 

原创粉丝点击