ZOJ 1276 Optimal Array Multiplication Sequence(矩阵连乘)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1276
裸的矩阵连乘问题。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
using namespace std; int n;
int p[];
int m[][], s[][]; void dp()
{
for (int i = ; i <= n; i++) m[i][i] = ;
for (int r = ; r <= n;r++)
for (int i = ; i <= n - r + ; i++)
{
int j = i + r - ;
m[i][j] = m[i + ][j] + p[i - ] * p[i] * p[j];
s[i][j] = i;
for (int k = i + ; k < j; k++)
{
int t = m[i][k] + m[k + ][j] + p[i - ] * p[k] * p[j];
if (t < m[i][j])
{
m[i][j] = t;
s[i][j] = k;
}
}
}
} void print(int i,int j)
{
if (i == j)
cout << "A" << i;
else
{
cout << "(";
print(i, s[i][j]);
cout << " x ";
print(s[i][j] + , j);
cout << ")";
}
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int kase = ;
while (cin >> n&& n)
{
for (int i = ; i <= n; i++)
cin >> p[i - ] >> p[i];
dp();
cout << "Case " << ++kase << ": ";
print(,n);
cout << endl;
}
return ;
}
ZOJ 1276 Optimal Array Multiplication Sequence(矩阵连乘)的更多相关文章
- 矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence
题目传送门 /* 题意:加上适当的括号,改变计算顺序使得总的计算次数最少 矩阵连乘积问题,DP解决:状态转移方程: dp[i][j] = min (dp[i][k] + dp[k+1][j] + p[ ...
- ZOJ 1276 "Optimal Array Multiplication Sequence"(最优矩阵链乘问题+区间DP)
传送门 •题意 矩阵 A(n×m) 和矩阵 B(m×k) 相乘,共做 n×m×k 次乘法运算: 给你 n 个矩阵,求这 n 个矩阵的最优结合方式,使得做的总乘法运算次数最少: •题解 定义dp(i,j ...
- UVAoj 348 - Optimal Array Multiplication Sequence
/* 题意:矩阵相乘的最少的步数 dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j]+num[i-1]*num[k]*num[j]); 表示的是第i个矩阵到第j个矩阵 ...
- UVA - 348Optimal Array Multiplication Sequence(递推)
id=19208">题目:Optimal Array Multiplication Sequence 题目大意:给出N个矩阵相乘.求这些矩阵相乘乘法次数最少的顺序. 解题思路:矩阵相乘 ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场
Matrix multiplication Time ...
- poj3519 Lucky Coins Sequence矩阵快速幂
Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu_5950_Recursive sequence(矩阵快速幂)
题目链接:hdu_5950_Recursive sequence 题意:递推求解:F(n) = 2*F(n-2) + F(n-1) + n4 和F(1) = a,F(2) = b: 题解: 一看数据范 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
随机推荐
- 09.Curator临时节点
使用Curator也可以简化Ephemeral Node (临时节点)的操作.临时节点驻存在ZooKeeper中,当连接和session断掉时被删除.比如通过ZooKeeper发布服务,服务启 ...
- jq和axios的取消请求
场景: 分页: 每次点击分页会发送请求,如果上一次请求还未获取到,下一次请求已经开始且先一步获取到,那么数据上会出现问题. 快速点击会发送多次请求,多次点击的时候一般的做法我们会定义一个flag,此时 ...
- js判断移动端和PC端跳转不同页面
方法一: /* * * 判断PC端与WAP端 */ var mobile_bs = { versions: function() { var u = navigator.userAgent; retu ...
- 传智播客微金所项目实战移动web开发
1.源码笔记 我的源码+笔记(很重要):链接: http://pan.baidu.com/s/1kULKqcJ 感谢传智播客项目相关视频:1.6天 链接: https://pan.baidu.com/ ...
- poj2176 Folding【区间DP】
Folding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1841 Accepted: 642 Special ...
- This module embeds Lua, via LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.
openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers https://github.com/openre ...
- Unity-DLL反编译学习
本文由博主(SunboyL)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/DLL_DeCompilation.html 在Unity实际开发过程中, ...
- PROPAGATION_REQUIRES_NEW VS PROPAGATION_NESTED
PROPAGATION_REQUIRES_NEW, in contrast to PROPAGATION_REQUIRED, uses a completely independent transac ...
- (2.4)DDL增强功能-数据汇总grouping、rollup、cube
参考:https://www.cnblogs.com/nikyxxx/archive/2012/11/27/2791001.html 1.rollup (1)rollup在group by 子句中使用 ...
- 【spring mvc】基础概念
1.容器 servlet容器 负责管理servlet生命周期. web容器–tomcat 负责管理和部署web应用,其本身可能具备servlet容器组件:如果没有,一般能将第三方servlet容器作为 ...