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) < ...
随机推荐
- $apply方法的作用
$apply方法是用来触发脏检查,它在控制器里监听一个变量,每当这个变量的值改变的时候,它会去与最初的值做一次比较,然后HTML页面就会及时更新该变量的值(将最新的值赋值到html页面的view层或M ...
- poj1144
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12521 Accepted: 5760 Descript ...
- JS insertAdajcentHTML 方法简介
修改节点的内容除了常用的innerHTML和innerText之外,还有insertAdjacentHTML和insertAdjacentText方法,可以在指定的地方插入内容.insertAdjac ...
- vbs test
'-----------------------------------Class clsGetProfilePrivate rootDocPublic Sub setProfile(strFileN ...
- ant exec
http://ant.apache.org/manual/Tasks/exec.html Exec Description Executes a system command. When the os ...
- 九度oj-1003-Java
题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式输出. 输入: 输入包含多组数据数据,每组数据占一行,由两 ...
- JNDI全面总结(zz)
原理: 在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- MBProgressHUD框架的使用:https://github.com/jdg/MBProgressHUD
MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgressHUD,然后把两个MBProgressHUD.h和MBP ...
- 物联网-手机远程控制家里的摄像头(2) - POP3和SMTP的C语言精简实现
在上一篇博客里面,使用了Python来发送.接收mail,但是实际测试中出现了一些不稳定的 情况,而且Python和即将使用的opencv会不兼容,使用进程间通讯或者其他方法会让整个系统 显得复杂而且 ...