id=19208">题目:Optimal Array Multiplication Sequence

题目大意:给出N个矩阵相乘。求这些矩阵相乘乘法次数最少的顺序。

解题思路:矩阵相乘不满足交换率但满足结合率。dp【i】【j】 代表第1个矩阵到第j个矩阵之间的最少的乘法次数,转移状态方程:dp【i】【j】 = Min(dp【i】【k】 + dp【k + 1】【j】  + A[i - 1] * A[k] *A[j]) k>= i && k <= j - 1。A0A1A2A3..Ak  |  Ak+1AK+2Aj, 第i个矩阵的行Ai - 1 ,列Ai。上面那个式子的意思是在K这个地方断开。两边的矩阵都先做相乘,这样左右两边的相乘是互不影响的。这样左右两边的相乘也是採取找最优的形式(最优的子结构)。全部就有上面的状态转移方程。最后是路径输出,我没有记录路径而是直接用递归的写法,可是这里要注意仅仅要找一种最优序列就好了(有的矩阵相乘存在多种最优序列),在递归中少了一个break害我wa了好久,找不到原因。

代码:

#include <cstdio>
#include <cstring> typedef long long ll; const int INF = 0x7fffffff;
const int N = 15; ll A[N];
int n;
ll dp[N][N]; ll Min (const ll a, const ll b) { return a < b? a: b;} void printf_ans (int s, int e) { if (s == e) { printf ("A%d", s);
return;
}
for (int i = s; i < e; i++) { if ((dp[s][i] + dp[i + 1][e] + A[s - 1] * A[i] * A[e]) == dp[s][e]) { if (s != i)
printf ("(");
printf_ans(s, i);
if (s != i)
printf (")");
printf (" x ");
if (i + 1 != e)
printf ("(");
printf_ans(i + 1, e);
if (i + 1 != e)
printf (")");
break;
}
}
} int main () { int cas = 0;
while (scanf ("%d", &n), n) { for (int i = 0; i < n; i++)
scanf ("%lld%lld", &A[i], &A[i + 1]); for (int i = 1; i <= n; i++)
dp[i][i] = 0; ll temp;
for (int len = 1; len < n; len++)
for (int i = 1; i + len <= n; i++) { temp = INF;
for (int k = 0; k < len; k++)
temp = Min (temp, dp[i][i + k] + dp[i + k + 1][i + len] + A[i - 1] * A[i + k] * A[i + len]);
dp[i][i + len] = temp;
} // printf ("%lld\n", dp[1][n]);
printf ("Case %d: (", ++cas);
printf_ans(1, n);
printf (")\n");
}
return 0;
}

UVA - 348Optimal Array Multiplication Sequence(递推)的更多相关文章

  1. 矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence

    题目传送门 /* 题意:加上适当的括号,改变计算顺序使得总的计算次数最少 矩阵连乘积问题,DP解决:状态转移方程: dp[i][j] = min (dp[i][k] + dp[k+1][j] + p[ ...

  2. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  3. UVA 11077 Find the Permutations 递推置换

                               Find the Permutations Sorting is one of the most used operations in real ...

  4. hdu-5496 Beauty of Sequence(递推)

    题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  5. UVA 11077 - Find the Permutations(递推)

    UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  6. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  8. UVA 557 Burger 排列组合递推

    When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at t ...

  9. UVA 10559 Blocks(区间DP&&递推)

    题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...

随机推荐

  1. 基于移动Web的视图引擎实现

    第一步:移动视图引擎实现 using System.Web.Mvc; /// <summary> /// 移动版View引擎 /// </summary> public cla ...

  2. 7.union

    联合结果集union 简单的结果集联合: select number,name,age from emp union select cardnumber,name,age from emp2 基本的原 ...

  3. Windows phone开发 页面布局之屏幕方向

    (博客部分内容参考Windows phone开发文档) Windows phone的屏幕方向是利用Windows phone设备的方向传感器提供的数据实现切换的. Windows Phone支持纵向和 ...

  4. 酷派 5267 刷入第三方 recovery教程 刷机 ROOT

    准备工作: 一台电脑: 酷派5267手机: 一张内存卡: 下载好刷机资料:  http://pan.baidu.com/s/1i4LoVh7 备用下载: http://pan.baidu.com/s/ ...

  5. 【MySQL】通信协议

    1.TCP/IP(Transmission Control Protocol/Internet Protocol) 该通信协议套件用于连接 Internet 上的主机.在 Linux 操作系统中,TC ...

  6. JDK自带工具

    工具名称 描述 appletviewer.exe 用于运行并浏览applet小程序. apt.exe 注解处理工具(Annotation Processing Tool),主要用于注解处理. extc ...

  7. Caffe2:使用Caffe构建LSTM网络

    前言: 一般所称的LSTM网络全叫全了应该是使用LSTM单元的RNN网络. 原文:(Caffe)LSTM层分析 入门篇:理解LSTM网络 LSTM的官方简介: http://deeplearning. ...

  8. 蛮好用的局域网测试工具iperf

    公司局域网总是莫名其妙的和一台机器网速很慢,虽然无法解决也无人解决,但是能有个有效的测试至少也会心里有数. 咱干不了网络硬件布线的活,就测测网速吧. 网上找了下,开始有文章介绍NetIQ Chario ...

  9. C# 检测字符串是否为数字

    long n; 1. ], ].All(char.IsDigit); //识别空字符时候 会认为是数字 string str0 = ""; string str1 = " ...

  10. wx:for

    .JSPage({ data: { input_data: [ { id: 1, unique: "unique1" }, { id: 2, unique: "uniqu ...