Multiplication Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8040   Accepted: 4979

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

http://www.cnblogs.com/hoodlum1980/archive/2012/06/07/2540150.html浙大童鞋的结题报告,看了人家的,越发自己就是一纯渣比
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int Max = + ;
const int INF = 0x3f3f3f3f;
int dp[Max][Max],card[Max];
int n;
void Input()
{
for(int i = ; i <= n; i++)
scanf("%d", &card[i]);
}
int solve()
{
memset(dp, , sizeof(dp));
for(int p = ; p < n; p++)
{
for(int i = ; i < n; i++)
{
int j = i + p;
if(j > n)
break;
dp[i][j] = INF;
for(int k = i + ; k < j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + card[i] * card[k] * card[j]);
}
}
}
return ;
}
int main()
{
while(scanf("%d", &n) != EOF)
{
Input();
solve();
printf("%d\n", dp[][n]);
}
return ;
}
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int Max = + ;
const int INF = 0x3f3f3f3f;
int dp[Max][Max],card[Max],trace[Max][Max]; //记录选择方案
int n;
void Input()
{
for(int i = ; i <= n; i++)
scanf("%d", &card[i]);
}
int solve()
{
memset(dp, , sizeof(dp));
memset(trace, , sizeof(trace));
for(int p = ; p < n; p++)
{
for(int i = ; i < n; i++)
{
int j = i + p;
if(j > n)
break;
dp[i][j] = INF;
for(int k = i + ; k < j; k++)
{
if(dp[i][j] > dp[i][k] + dp[k][j] + card[i] * card[k] * card[j])
{
dp[i][j] = dp[i][k] + dp[k][j] + card[i] * card[k] * card[j];
trace[i][j] = k;
}
}
}
}
return ;
}
void print(int Begin, int End)
{
if(End - Begin <= )
return ;
printf("%d ", trace[Begin][End]); //结果是逆序的
print(Begin, trace[Begin][End]);
print(trace[Begin][End], End);
}
int main()
{
while(scanf("%d", &n) != EOF)
{
Input();
solve();
printf("%d\n", dp[][n]);
print(,n);
}
return ;
}

输出选择方案

												

POJ1651Multiplication Puzzle(矩阵链乘变形)的更多相关文章

  1. POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP

    题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65 ...

  2. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  3. 【UVa-442】矩阵链乘——简单栈练习

    题目描述: 输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. Sample Input 9 A 50 10 B 10 20 C 20 5 D 30 35 E ...

  4. POJ1260 Pearls(dp,矩阵链乘法)

    题目链接. 题目大意: 给定一个n,和两个序列a[i], p[i]. a[i] 表示需要购买 i品质 的数量,p[i] i 等级的价格. 1.每个品质都会有不同的价格,价格依据品质上升而上升 2.买一 ...

  5. COJ 0016 20603矩阵链乘

    传送门:http://oj.cnuschool.org.cn/oj/home/solution.htm?solutionID=35454 20603矩阵链乘 难度级别:B: 运行时间限制:1000ms ...

  6. Algorithm --> 矩阵链乘法

    动态规划--矩阵链乘法 1.矩阵乘法       Note:只有当矩阵A的列数与矩阵B的行数相等时A×B才有意义.一个m×r的矩阵A左乘一个r×n的矩阵B,会得到一个m×n的矩阵C. #include ...

  7. CODEVS 3546 矩阵链乘法

    http://codevs.cn/problem/3546/ 题目 给定有n个要相乘的矩阵构成的序列(链)<A1,A2,A3,.......,An>,要计算乘积A1A2.....An.一组 ...

  8. UVa 10003 切木棍(区间DP+最优矩阵链乘)

    https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...

  9. (最大矩阵链乘)Matrix-chain product

    Matrix-chain product. The following are some instances. a)       <3, 5, 2, 1,10> b)       < ...

随机推荐

  1. sasscore学习之_mixin.scss

    _mixin scss包括常用的mixin,%及@functionmixin,通过@include调用,样式通过拷贝的方式使用,尤其适用于传递参数%,通过@extend调用,样式通过组合申明的方式使用 ...

  2. 键盘事件keydown、keypress、keyup随笔整理总结(摘抄)

    原文1:http://www.cnblogs.com/silence516/archive/2013/01/25/2876611.html 原文2:http://www.cnblogs.com/leo ...

  3. 【WPF】无边框窗体

    之前写了一个支持尺寸变换的无边框窗体的一个基窗体,代码如下: public class LBaseWindow : Window { /// <summary> /// 基窗体 /// & ...

  4. 【Windows phone 8】欢迎引导页面02

    [目标]前一篇文章已经实现了图片的切换,这里需要限制pivot的循环滚动. [思路]通过手势事件,对第一张,最后一张图片处加以限制 [前台] 在pivot处加上 <toolkit:Gesture ...

  5. ffplay 参数说明分享

    ffplay 使用参数说明分享 E:\SRCFORTEST\software\ffmpeg-20131021\ffmpeg-20131021-git-712eff4-win32-static\ bin ...

  6. 流程引擎Activiti系列:如何将kft-activiti-demo-no-maven改用mysql数据库

    kft-activiti-demo-no-maven这个工程默认使用h2数据库,这是一个内存数据库,每次启动之后都要重新对数据库做初始化,很麻烦,所以决定改用mysql,主要做3件事情: 1)在mys ...

  7. Linux第四次学习笔记

    程序的机器级表示 寻址方式的演变 DOS → 8086 → IA32 Inter处理器系列俗称x86,其演变过程(根据其所需要的晶体管数量来说明): 8086 → 80286 → i386 → i48 ...

  8. groot 引入外部模板

    index7.html <html><head> <title>groots引入外部模板van</title> <script src=" ...

  9. 【团队项目演示】FZU5BOYS之团队项目链接汇总

    FZU5BOYS      项目冲刺之博客汇总 Alpha版本 Day One Day Two Day Three Day Four Day Five Day Six Day Seven Day Ei ...

  10. Easyui使用记录

    一天就这搞了这几行. 1. if else 可以嵌套: 2. 子页面调用父页面js,需要使用top.父页面js的方法. <script type="text/javascript&qu ...