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) < ...
随机推荐
- linux下安装php的imagick扩展模块(附php升级脚本)
imagick是一个PHP的扩展,是一套软件系列,用ImageMagick提供的API来进行图片的创建与修改,不过这些操作已经包装到扩展imagick中去了,最终调用的是ImageMagick提供的A ...
- 深入理解abstract class和interface(转)
原文地址 深入理解abstract class和interface java提高篇(四)-----抽象类与接口
- Asp.net用户控件和委托事件
在Asp.net系统制作过程中,门户类型的网站,我们可以用DIV+CSS+JS+Ajax全部搞定,但是一旦遇到界面元素比较复杂的时候,还是UserControl比较方便一些,各种封装,各种处理,然后拖 ...
- struct2cell
函数功能:把结构体转换为元胞数组. 语法格式: c = struct2cell(s) 如果s是m*n(m行n列)的二维的结构体数组,每个结构体含有p个域,则转换得到一个p*m*n的元胞数组c. 如果s ...
- mysql命令行
mysql -u root -p create database bookstore; drop database bookstore; use bookstore create table user ...
- initializer for conditional binding must have optional type not AVAudioPlayer
if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") { ...
- virtualbox 打不开ubuntu解决
装了一个win7x64,准备打开ubuntu12.04,后来竟然报错(最新版的virtualbox,VirtualBox-4.3.18-96516-Win): 也没找到什么原因,网上查了之后,禁用了w ...
- 给ubuntu开通FTP功能
一.安装vsftp安装: sudo apt-get install vsftpd 二.启动.停止.重启vsftp 启动vsftp:sudo service vsftpd start 三.创建ftp用户 ...
- JPush (极光推送) For Xamarin.Android
官方教程上讲的是 GCM (Google Cloud Messaging) , 不过 GFW 是 GCM 过不去的坎. 极光推送 JPush 是国内的一个不错的替代方案. JPush 提供的 API ...
- 关于json 与 Request Header 的Content-Type 一些关系。
由于最近遇到关于,ashx文件ajax解析参数的问题.查询网上很多资料后,已经解决. 鉴于网上已经足够多的,关于这个问题的文章.大部分内容来自互联网,我这里只是做一些整理和记录.特此说明并非原创. C ...