UVa 348 Optimal Array multiplication Sequence: Interval dp& matrix chain multiply, MCM

Source: Internet
Author: User
Tags cas printf time limit

348-optimal Array Multiplication Sequence

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=284

Memory Search: dp[a][b] = max (Dp[a][b], Dp[a][i] + dp[i + 1][b] + x[a] * y[i] * y[b])

Complete code:

/*0.089s*/#include <cstdio> #include <cstring> const int MAXN = 15;  
    
int X[MAXN], Y[MAXN], D[MAXN][MAXN], PATH[MAXN][MAXN];  
    the int dp (int a, int b) {if (D[a][b] >= 0) return d[a][b];  
    PATH[A][B] = A;  
    if (a = = B) return d[a][b] = 0;  
    D[A][B] = -1u >> 1;  
    int tmp;  
        for (int i = A; I < b; ++i) {TMP = DP (A, I) + DP (i + 1, b) + x[a] * y[i] * y[b];  
    if (TMP < D[A][B]) d[a][b] = tmp, path[a][b] = i;  
return d[a][b];  
    } void print (int a, int b) {if (a > B) return;  
    if (a = = b) printf ("a%d", A + 1);  
        else {printf ("(");  
        Print (A, path[a][b]);  
        printf ("x");  
        Print (Path[a][b] + 1, b);  
    printf (")");  
    int main () {int n, cas = 0;  
        while (scanf ("%d", &n), N) {memset (d,-1, sizeof (d));  
  for (int i = 0; i < n; i++)          scanf ("%d%d", &x[i], &y[i]);  
        DP (0, N-1);  
        printf ("Case%d:", ++cas);  
        Print (0, n-1);  
    Putchar (10);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.