poj 1651 Multiplication Puzzle【区间DP】

题目链接:http://poj.org/problem?id=1651

题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n)
同时ans+=a[pos-1]*a[pos]*a[pos+1],一直消元素直到最后剩余2个,求方案最小的ans是多少?

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <assert.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;

const long long inf = 1e18;

int n;
int a[110];
long long dp[110][110];

int main()
{
    while (cin>>n)
    {
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        memset(dp, 0, sizeof(dp));
        for (int k = 2; k <= n - 1; k++)//区间长度
        {
            for (int i = 1; i + k <= n; i++)//区间起点
            {
                int j = i + k;//区间终点
                dp[i][j] = inf;
                for (int r = i + 1; r < j; r++)
                {
                    dp[i][j] = min(dp[i][j], dp[i][r] + dp[r][j] + a[i] * a[r] * a[j]);
                }
            }
        }
        cout << dp[1][n] << endl;
    }
    return 0;
}

你可能感兴趣的:(poj 1651 Multiplication Puzzle【区间DP】)