POJ1651Multiplication Puzzle(矩阵链乘变形)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8040 | Accepted: 4979 |
Description
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
Output
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(矩阵链乘变形)的更多相关文章
- POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP
题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65 ...
- POJ1651Multiplication Puzzle[区间DP]
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8737 Accepted: ...
- 【UVa-442】矩阵链乘——简单栈练习
题目描述: 输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. Sample Input 9 A 50 10 B 10 20 C 20 5 D 30 35 E ...
- POJ1260 Pearls(dp,矩阵链乘法)
题目链接. 题目大意: 给定一个n,和两个序列a[i], p[i]. a[i] 表示需要购买 i品质 的数量,p[i] i 等级的价格. 1.每个品质都会有不同的价格,价格依据品质上升而上升 2.买一 ...
- COJ 0016 20603矩阵链乘
传送门:http://oj.cnuschool.org.cn/oj/home/solution.htm?solutionID=35454 20603矩阵链乘 难度级别:B: 运行时间限制:1000ms ...
- Algorithm --> 矩阵链乘法
动态规划--矩阵链乘法 1.矩阵乘法 Note:只有当矩阵A的列数与矩阵B的行数相等时A×B才有意义.一个m×r的矩阵A左乘一个r×n的矩阵B,会得到一个m×n的矩阵C. #include ...
- CODEVS 3546 矩阵链乘法
http://codevs.cn/problem/3546/ 题目 给定有n个要相乘的矩阵构成的序列(链)<A1,A2,A3,.......,An>,要计算乘积A1A2.....An.一组 ...
- UVa 10003 切木棍(区间DP+最优矩阵链乘)
https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...
- (最大矩阵链乘)Matrix-chain product
Matrix-chain product. The following are some instances. a) <3, 5, 2, 1,10> b) < ...
随机推荐
- 对比git rm和rm的使用区别
在这里说一下git rm和rm的区别,虽然觉得这个问题有点肤浅,但对于刚接触git不久的朋友来说还是有必要的. 用 git rm 来删除文件,同时还会将这个删除操作记录下来:用 rm 来删除文件,仅仅 ...
- 【WPF】FillRule
获取或设置如何组合此 GeometryGroup 中所包含对象的相交区域. Dependency property identifier field: FillRuleProperty FillRul ...
- WCF 异步调用问题
添加引用时生成"勾选允许生成异步操作" Wcf异步调用三种方式: 第一种:直接调用异步方法 var serviceClient = new MyServiceClient(); s ...
- python 线性回归示例
说明:此文的第一部分参考了这里 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子.scipy.stats.linregress例子.panda ...
- 拿什么拯救你,我的代码--c#编码规范实战篇 (转)
http://www.cnblogs.com/lazio10000/p/5413439.html 此文为译文,原文地址请点击. 本文通过重构一个垃圾代码,阐述了如何写出优秀的代码.开发人员及代码审核人 ...
- CSS 实现加载动画之一-菊花旋转
最近打算整理几个动画样式,最常见的就是我们用到的加载动画.加载动画的效果处理的好,会给页面带来画龙点睛的作用,而使用户愿意去等待.而页面中最常用的做法是把动画做成gif格式,当做背景图或是img标签来 ...
- 简单通用JDBC辅助类封装
哎,最近很好久没在博客园写点东西了,由于工作的原因,接触公司自己研发的底层orm框架,偶然发现该框架在调用jdbc操作的时候参考的是hibernate 里面的SimpleJdbcTemplate,这里 ...
- LeetCode:Clone Graph
题目如下:实现克隆图的算法 题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...
- poj2154-color-polyan次二面体+欧拉函数优化
N<=1e9,O(nlogn)的做法会超时.从枚举置换转变为枚举轮换长度,然后可以利用欧拉函数,把复杂度变为O(√n * logn) /*---------------------------- ...
- 20145215《Java程序设计》第8周学习总结
20145215<Java程序设计>第八周学习总结 教材学习内容总结 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以设定缓冲区(Bu ...