矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence

题目传送门

 1 /*
 2     题意:加上适当的括号,改变计算顺序使得总的计算次数最少
 3     矩阵连乘积问题,DP解决:状态转移方程:
 4     dp[i][j] = min (dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j])    (i<=k<j)
 5     s[i][j] 记录断开的地方(即加括号的位置),回溯法输出结果
 6 */
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <string>
10 #include <algorithm>
11 #include <cmath>
12 #include <iostream>
13 using namespace std;
14
15 const int MAXN = 1e2 + 10;
16 const int INF = 0x3f3f3f3f;
17 int dp[MAXN][MAXN];
18 int s[MAXN][MAXN];
19 int p[MAXN];
20 int n;
21
22 void print(int i, int j)
23 {
24     if (i == j)    printf ("A%d", i);
25     else
26     {
27         printf ("(");
28         print (i, s[i][j]);
29         printf (" x ");
30         print (s[i][j] + 1, j);
31         printf (")");
32     }
33 }
34
35 void work(void)
36 {
37     for (int i=1; i<=n; ++i)    dp[i][i] = 0;
38     for (int l=2; l<=n; ++l)
39     {
40         for (int i=1; i<=n-l+1; ++i)
41         {
42             int j = i + l - 1;
43             dp[i][j] = INF;
44             for (int k=i; k<=j-1; ++k)
45             {
46                 int tmp = dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j];
47                 if (tmp < dp[i][j])
48                 {
49                     dp[i][j] = tmp;    s[i][j] = k;
50                 }
51             }
52         }
53     }
54
55     print (1, n);    puts ("");
56 }
57
58 int main(void)        //ZOJ 1276 Optimal Array Multiplication Sequence
59 {
60     //freopen ("ZOJ_1276.in", "r", stdin);
61
62     int cas = 0;
63     while (scanf ("%d", &n) == 1)
64     {
65         if (n == 0)    break;
66         for (int i=1; i<=n; ++i)    scanf ("%d%d", &p[i-1], &p[i]);
67
68         printf ("Case %d: ", ++cas);
69         work ();
70     }
71
72     return 0;
73 }
74
75 /*
76 Case 1: (A1 x (A2 x A3))
77 Case 2: ((A1 x A2) x A3)
78 Case 3: ((A1 x (A2 x A3)) x ((A4 x A5) x A6))
79 */
时间: 2024-08-26 04:56:13

矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence的相关文章

UVA 348 &amp; ZOJ 1276 Optimal Array Multiplication Sequence(dp , 矩阵链相乘问题)

Optimal Array Multiplication Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Given two arrays A and B, we can determine the array C = AB using the standard definition of matrix multiplication: The number of

UVA 348 Optimal Array Multiplication Sequence(最优矩阵链乘)

L - Optimal Array Multiplication Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 348 Appoint description:  acmparand  (2013-08-02)System Crawler  (2015-08-04) Description Given two arrays A a

UVAoj 348 - Optimal Array Multiplication Sequence

1 /* 2 题意:矩阵相乘的最少的步数 3 dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j]+num[i-1]*num[k]*num[j]); 4 表示的是第i个矩阵到第j个矩阵相乘的最少步数 5 sign[i][j]表示的是第i个矩阵到第j个矩阵相乘的最少步数是由第i个矩阵到第sign[i][j]个矩阵相乘最少步数 6 和第sign[i][j]+1个矩阵到第j个矩阵相乘最少步数的得到的最小值! 7 */ 8 #include<iostream> 9 #i

uva 3485 Optimal Array Multiplication Sequence

题目: I - Optimal Array Multiplication Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 348 Description Given two arrays A and B, we can determine the array C = AB using the standard definition 

UVa 348 - Optimal Array Multiplication Sequence

题目:矩阵连乘,求最小运算次数,输出运算优先级(用括号给出). 分析:dp,区间动态规划. 状态:设DP[ l ][ s ]为以 s 开始长度为 l 的区间的 矩阵乘积的最小值: 阶段:区间长度: 决策:DP[ l ][ s ] = min(DP[ k ][ s ] + DP[ l-k ][ s+k ] + 乘法代价){ 1<k<l }: 记录每一步运算的位置,递归输出计算方式即可. 说明:zoj1276(2011-09-19 01:38). #include <iostream>

AYITACM2016省赛第三周I - Optimal Array Multiplication Sequence(dp)

矩阵最少乘法 题意: 给你2个矩阵A.B,我们使用标准的矩阵相乘定义C=AB如下: A阵列中栏(column)的数目一定要等于B阵列中列(row)的数目才可以做此2阵列的相乘.若我们以rows(A),columns(A)分别代表A阵列中列及栏的数目,要计算C阵列共需要的乘法的数目为:rows(A)*columns(B)*columns(A).例如:A阵列是一个10x20的矩阵,B阵列是个20x15的矩阵,那么要算出C阵列需要做10*15*20,也就是3000次乘法. 要计算超过2个以上的矩阵相乘

uva348Optimal Array Multiplication Sequence (最优矩阵链乘+路径输出)

Optimal Array Multiplication Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 348 Appoint description: Description Download as PDF Given two arrays A and B, we can determine the array C = AB using the

UVA - 348Optimal Array Multiplication Sequence(递推)

题目: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+1

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matrices A and B of size n×n, find the product of them. b