POJ 1651:Multiplication Puzzle 矩阵相乘式DP

Multiplication Puzzle

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7118   Accepted: 4385

Description

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on
the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring

10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000

If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be

1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

Input

The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.

Output

Output must contain a single integer - the minimal score.

Sample Input

6
10 1 50 50 20 5

Sample Output

3650

题意就是给了几个数,依次拿走中间的数,每一次拿走第i个数都会有相应的代价value[i-1]*value[i]*value[i+1],问要求的是给定的序列 拿走所有中间的数 代价最少是多少。

就是矩阵相乘式DP。。。把那个数单提出来想象成要把它第一个拿走就OK了啊,怎么自己的脑筋总是奔到死胡同里面呢,气死我了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

#define INF 0x3f3f3f3f

int dp[105][105];
int value[105];
int num;

int main()
{
	int i,j,k,len;
	scanf("%d",&num);

	for(i=1;i<=num;i++)
	{
		scanf("%d",&value[i]);
		dp[i][i]=0;
	}

	for(i=1;i<=num;i++)
	{
		dp[i][i+1] = 0;
		dp[i][i+2] = value[i]*value[i+1]*value[i+2];
	}

	for(len=3;len<=num;len++)
	{
		for(i=1,j=len+i;j<=num;i++)
		{
			j=len+i;
			dp[i][j]=INF;
			for(k=i+1;k<j;k++)
			{
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+value[k]*value[i]*value[j]);//想象成是第一个拿走的
			}
		}
	}
	cout<<dp[1][num]<<endl;
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 13:53:49

POJ 1651:Multiplication Puzzle 矩阵相乘式DP的相关文章

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move

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> #inc

poj - 1651 - Multiplication Puzzle(dp)

题意:n个数(3 <= N <= 100)的序列,每次取一个数(不可以取最左最右)a[k],这时得到一个权值为a[k]左边的数 * a[k] * a[k]右边的数,问最小权值和. 题目链接:http://poj.org/problem?id=1651 -->>状态:dp[i][j]表示第i个数到第j个数组成的序列的最小权值和. 状态转移方程:dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[k] * a[j]);(枚举最

POJ 1651 Multiplication Puzzle 题解 区间DP

题目链接:http://poj.org/problem?id=1651[题目描述]<乘法谜题>乘法谜题源自这样一个背景,我们有一行 n 张牌,平铺在桌面上,每张牌的牌面上都标有一个正整数.玩家的初始得分是 0,他接下来要进行 n-2 次操作,每次操作他都需要从桌面上取出一张牌,然后他的得分会加上他取出的这张牌与取出的这张牌的左边的牌以及取出的这张牌的右边的牌的乘积.在第 n-2 次操作结束后,桌面上将会只剩下两张牌.你的目的是帮助玩家决定取牌的顺序,使得玩家的最终得分最小.举个例子,如果一开始

POJ 1651 Multiplication Puzzle 区间DP

题意:在一个数字序列中, 取出不包括头和尾的所有数字, 每次取出一个数字的代价等于取出的这个数和左右两个数的乘积, 求总代价和最小的取法 此小到大递归 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <iostrea

POJ 1651 Multiplication Puzzle 区间dp(水

题目链接:点击打开链 题意: 给定一个数组,每次可以选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp[l,r] = min( dp[l, i] + dp[i, r] + a[l] * a[i] * a[r]); #include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <cst

POJ 1651 Multiplication Puzzle

区间DP.dp[i][j]表示(i,j)开区间内全部取走的最小值. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int INF=0x7FFFFFFF; const int maxn=100+10; int a[maxn]; int dp[maxn][maxn]; int n; void init() { fo

hdu 4920 Matrix multiplication(矩阵相乘)多校训练第5场

Matrix multiplication                                                                           Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Given two matrices A and B of size n×n, find the