zoukankan      html  css  js  c++  java
  • POJ1651Multiplication Puzzle(矩阵链乘变形)

    Multiplication Puzzle
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8040   Accepted: 4979

    Description

    The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

    The goal is to take cards in such order as to minimize the total number of scored points.

    For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
    10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000

    If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
    1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

    Input

    The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.

    Output

    Output must contain a single integer - the minimal score.

    Sample Input

    6
    10 1 50 50 20 5
    

    Sample Output

    3650

    http://www.cnblogs.com/hoodlum1980/archive/2012/06/07/2540150.html
    浙大童鞋的结题报告,看了人家的,越发自己就是一纯渣比
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 const int Max = 100 + 5;
     7 const int INF = 0x3f3f3f3f;
     8 int dp[Max][Max],card[Max];
     9 int n;
    10 void Input()
    11 {
    12     for(int i = 1; i <= n; i++)
    13         scanf("%d", &card[i]);
    14 }
    15 int solve()
    16 {
    17     memset(dp, 0, sizeof(dp));
    18     for(int p = 2; p < n; p++)
    19     {
    20         for(int i = 1; i < n; i++)
    21         {
    22             int j = i + p;
    23             if(j > n)
    24                 break;
    25             dp[i][j] = INF;
    26             for(int k = i + 1; k < j; k++)
    27             {
    28                 dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + card[i] * card[k] * card[j]);
    29             }
    30         }
    31     }
    32     return 0;
    33 }
    34 int main()
    35 {
    36     while(scanf("%d", &n) != EOF)
    37     {
    38         Input();
    39         solve();
    40         printf("%d
    ", dp[1][n]);
    41     }
    42     return 0;
    43 }
    View Code
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 const int Max = 100 + 5;
     7 const int INF = 0x3f3f3f3f;
     8 int dp[Max][Max],card[Max],trace[Max][Max]; //记录选择方案
     9 int n;
    10 void Input()
    11 {
    12     for(int i = 1; i <= n; i++)
    13         scanf("%d", &card[i]);
    14 }
    15 int solve()
    16 {
    17     memset(dp, 0, sizeof(dp));
    18     memset(trace, 0, sizeof(trace));
    19     for(int p = 2; p < n; p++)
    20     {
    21         for(int i = 1; i < n; i++)
    22         {
    23             int j = i + p;
    24             if(j > n)
    25                 break;
    26             dp[i][j] = INF;
    27             for(int k = i + 1; k < j; k++)
    28             {
    29                 if(dp[i][j] > dp[i][k] + dp[k][j] + card[i] * card[k] * card[j])
    30                 {
    31                     dp[i][j] =  dp[i][k] + dp[k][j] + card[i] * card[k] * card[j];
    32                     trace[i][j] = k;
    33                 }
    34             }
    35         }
    36     }
    37     return 0;
    38 }
    39 void print(int Begin, int End)
    40 {
    41     if(End - Begin <= 1)
    42         return ;
    43     printf("%d ", trace[Begin][End]);  //结果是逆序的
    44     print(Begin, trace[Begin][End]);
    45     print(trace[Begin][End], End);
    46 }
    47 int main()
    48 {
    49     while(scanf("%d", &n) != EOF)
    50     {
    51         Input();
    52         solve();
    53         printf("%d
    ", dp[1][n]);
    54         print(1,n);
    55     }
    56     return 0;
    57 }
    输出选择方案

  • 相关阅读:
    μTorrent for Windows
    坚决抵制Rarbg的恶意修改版
    Check YouTube Video Restrictions Online
    Questions about "Computer Systems: A Programmer's Perspective"
    los diccionarios que te acercan al mundo
    sqlserver 备份数据库 按时间命名备份文件
    Linux中的硬链接和软链接的概念、区别及用法
    高恪固件端口映射怎么配置
    C++中的DLL,Delphi调用需要注意之一
    Delphi中主线程与子线程调用同一方法同步问题
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5183952.html
Copyright © 2011-2022 走看看