• 【动态规划】XMU 1029 矩阵链乘法


    题目链接:

      http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1029

    题目大意

      题同乘法难题。给n+1个数,头尾不能动,中间的数可取出,取出时代价是现在该数和左右的乘积。求最小代价。

    题目思路:

      【动态规划】

      区间动规。

      f[i][j]表示从i到j的最小代价。

      枚举区间[i,j]中最后取出的是第k个数,则转移方程为f[i][k]+f[k][j]+a[i]*a[k]*a[j]

     1 //
     2 //by coolxxx
     3 //
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<memory.h>
     9 #include<time.h>
    10 #include<stdio.h>
    11 #include<stdlib.h>
    12 #include<string.h>
    13 //#include<stdbool.h>
    14 #include<math.h>
    15 #define min(a,b) ((a)<(b)?(a):(b))
    16 #define max(a,b) ((a)>(b)?(a):(b))
    17 #define abs(a) ((a)>0?(a):(-(a)))
    18 #define lowbit(a) (a&(-a))
    19 #define sqr(a) ((a)*(a))
    20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    21 #define eps (1e-8)
    22 #define J 10000000
    23 #define MAX 0x7f7f7f7f
    24 #define PI 3.1415926535897
    25 #define N 204
    26 using namespace std;
    27 typedef long long LL;
    28 int cas,cass;
    29 int n,m,lll,ans;
    30 int a[N];
    31 int f[N][N];
    32 int main()
    33 {
    34     #ifndef ONLINE_JUDGE
    35     freopen("1.txt","r",stdin);
    36 //    freopen("2.txt","w",stdout);
    37     #endif
    38     int i,j,l;
    39 //    for(scanf("%d",&cas);cas;cas--)
    40 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    41 //    while(~scanf("%s",s))
    42     while(~scanf("%d",&n))
    43     {
    44         memset(f,0x7f,sizeof(f));
    45         for(i=1;i<=n+1;i++)
    46         {
    47             scanf("%d",&a[i]);
    48             f[i][i]=f[i][i+1]=0;
    49         }
    50         for(l=2;l<=n;l++)
    51             for(i=1;i+l<=n+1;i++)
    52                 for(j=i;j<=i+l;j++)
    53                     f[i][i+l]=min(f[i][i+l],f[i][j]+f[j][i+l]+a[i]*a[j]*a[i+l]);
    54 
    55         printf("%d
    ",f[1][n+1]);
    56     }
    57     return 0;
    58 }
    59 /*
    60 //
    61 
    62 //
    63 */
    千万不要点
  • 相关阅读:
    初始Dubbo
    ProcessBuilder执行本地命令
    Deep Learning的基本思想
    机器学习(Machine Learning)
    Group By和Order By的总结
    Oracle sqlldr命令
    redis的简单操作
    Java程序中做字符串拼接时可以使用的MessageFormat.format
    Bean的作用域
    DI延伸
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5650492.html
Copyright © 2020-2023  润新知