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. bzoj 4318 OSU! —— 期望DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4318 期望DP,因为平方的期望不等于期望的平方,所以用公式递推: 第一次推错了囧,还是看这位 ...

  2. 线性预测与Levinson-Durbin算法实现

    在学习信号处理的时候,线性预测是一个比较难理解的知识点,为了加快很多朋友的理解,这里给出Levinson-Durbin算法的线性预测实现和一个测试Demo,Demo中很明确的把输入信号.预测信号.预测 ...

  3. A - HQ9+

    Problem description HQ9+ is a joke programming language which has only four one-character instructio ...

  4. Spring Cloud (1) 服务的注册与发现(Eureka)

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...

  5. spring 九种设计模式

    spring中常用的设计模式达到九种,我们举例说明: 第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一 ...

  6. JavaScript 判断手机端操作系统(Andorid/IOS)

    androidURL = "http://xxx/xxx.apk"; var browser = { versions: function() { var u = navigato ...

  7. Java创建Excel-DEMO

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Out ...

  8. Spring JPA 简单配置使用

    JPA 常用配置: # JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration) spring.data.jpa.repositories.b ...

  9. Java关于反射的用法

    一. 首先是准备一个需要反射的类 public class Person { private String name; private int age; public String sex; publ ...

  10. redis-linux

    redis3.0.4 server版本 jedis-2.7.2.jar spring-data-redis-1.6.0.RELEASE.jar commons-pool2-2.3.jar spring ...