青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

ZOJ 1276 Optimal Array Multiplication Sequence 經典DP問題

Given two arrays A and B, we can determine the array C = A B using the standard definition of matrix multiplication:

 

The number of columns in the A array must be the same as the number of rows in the B array. Notationally, let's say that rows(A) and columns(A) are the number of rows and columns, respectively, in the A array. The number of individual multiplications required to compute the entire C array (which will have the same number of rows as A and the same number of columns as B) is then rows(A) columns(B) columns(A). For example, if A is a 10 x 20 array, and B is a 20 x 15 array, it will take 10 x 15 x 20, or 3000 multiplications to compute the C array.


To perform multiplication of more than two arrays we have a choice of how to proceed. For example, if X, Y, and Z are arrays, then to compute X Y Z we could either compute (X Y) Z or X (Y Z). Suppose X is a 5 x 10 array, Y is a 10 x 20 array, and Z is a 20 x 35 array. Let's look at the number of multiplications required to compute the product using the two different sequences:

 

(X Y) Z


5 x 20 x 10 = 1000 multiplications to determine the product (X Y), a 5 x 20 array.

Then 5 x 35 x 20 = 3500 multiplications to determine the final result.

Total multiplications: 4500.

X (Y Z)

10 x 35 x 20 = 7000 multiplications to determine the product (Y Z), a 10 x 35 array.

Then 5 x 35 x 10 = 1750 multiplications to determine the final result.

Total multiplications: 8750.

Clearly we'll be able to compute (X Y) Z using fewer individual multiplications.

Given the size of each array in a sequence of arrays to be multiplied, you are to determine an optimal computational sequence. Optimality, for this problem, is relative to the number of individual multiplcations required.


Input

For each array in the multiple sequences of arrays to be multiplied you will be given only the dimensions of the array. Each sequence will consist of an integer N which indicates the number of arrays to be multiplied, and then N pairs of integers, each pair giving the number of rows and columns in an array; the order in which the dimensions are given is the same as the order in which the arrays are to be multiplied. A value of zero for N indicates the end of the input. N will be no larger than 10.


Output

Assume the arrays are named A1, A2, ..., AN. Your output for each input case is to be a line containing a parenthesized expression clearly indicating the order in which the arrays are to be multiplied. Prefix the output for each case with the case number (they are sequentially numbered, starting with 1). Your output should strongly resemble that shown in the samples shown below. If, by chance, there are multiple correct sequences, any of these will be accepted as a valid answer.


Sample Input

3
1 5
5 20
20 1
3
5 10
10 20
20 35
6
30 35
35 15
15 5
5 10
10 20
20 25
0


Sample Output

Case 1: (A1 x (A2 x A3))
Case 2: ((A1 x A2) x A3)
Case 3: ((A1 x (A2 x A3)) x ((A4 x A5) x A6))


Source: North Central North America 1996
    

    設計算矩陣A[i:j],1<=i<=j<=n,所需要的最少乘法次數為m[i,j],則原問題的最優值為m[1,n]。
    當i=j時,A[i:j]=Ai,m[i,i]=0,i=1,2,...,n;
    當i<j時,m[i,j]=m[i,k]+m[k+1][j]+pi-1*pk*pj,i<=k<j。
#include<iostream>
using namespace std;

void MatrixChain(int n,int p[],int m[][11],int s[][11]){
    
int i,j,k,r,t;
    
for(i=1;i<=n;i++) m[i][i]=0;
    
for(r=2;r<=n;r++)
        
for(i=1;i<=n-r+1;i++){
            j
=i+r-1;
            m[i][j]
=m[i+1][j]+p[i-1]*p[i]*p[j];
            s[i][j]
=i;
            
for(k=i+1;k<j;k++){
                t
=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
                
if(t<m[i][j]){
                    m[i][j]
=t;
                    s[i][j]
=k;
                }

            }

        }

}

void output(int i,int j,int s[][11]){
    
if(i==j)
        printf(
"A%d",i);
    
else{
        printf(
"(");
        output(i,s[i][j],s);
        printf(
" x ");
        output(s[i][j]
+1,j,s);
        printf(
")");
    }

}

int main(){
    
int i,n,ca=1,p[11],m[11][11],s[11][11];
    
while(scanf("%d",&n),n){
        
for(i=1;i<=n;i++) scanf("%d %d",&p[i-1],&p[i]);
        MatrixChain(n,p,m,s);
        printf(
"Case %d: ",ca++);
        output(
1,n,s);
        printf(
"\n");
    }

    
return 0;
}

posted on 2009-06-19 09:27 極限定律 閱讀(1220) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

導航

統計

常用鏈接

留言簿(10)

隨筆分類

隨筆檔案

友情鏈接

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲第一在线综合网站| 久久九九国产精品怡红院| 欧美一区二区三区在线视频| 一本色道久久综合亚洲91| 亚洲色图在线视频| 国产精品视频一区二区高潮| 亚洲国产视频直播| 黑人巨大精品欧美黑白配亚洲 | 国产精品天美传媒入口| 午夜精品久久久久久久久久久| 亚洲欧美日韩在线综合| 亚洲国产色一区| 国产精品热久久久久夜色精品三区| 午夜久久久久久| 亚洲免费成人av电影| 久久久综合精品| 亚洲尤物影院| 久久久久网站| 亚洲欧美国产日韩中文字幕| 亚洲国产日韩欧美综合久久| 国产视频一区在线观看| 欧美日韩午夜视频在线观看| 欧美88av| 日韩视频在线观看国产| 欧美激情1区2区| 欧美激情亚洲国产| 玖玖玖国产精品| 美女脱光内衣内裤视频久久网站| 一本色道久久综合| 亚洲一级黄色av| 另类av导航| 久久亚洲精选| 亚洲免费av电影| 先锋影音国产精品| 美女尤物久久精品| 欧美激情第4页| 亚洲专区一二三| 国产精品美女www爽爽爽| 午夜精品一区二区三区电影天堂 | 亚洲视频导航| 欧美不卡一区| 一区二区三区视频观看| 一本久久综合亚洲鲁鲁| 国产午夜精品一区理论片飘花| 欧美午夜美女看片| 在线免费日韩片| 免费在线播放第一区高清av| 午夜欧美不卡精品aaaaa| 欧美gay视频激情| 国产精品欧美日韩一区| 亚洲图片欧洲图片日韩av| 久久久亚洲一区| 亚洲一区二区视频在线| 欧美精品少妇一区二区三区| 伊人久久婷婷| 免费亚洲婷婷| 久久九九有精品国产23| 国产精品捆绑调教| 亚洲美女电影在线| 亚洲图片在线观看| 久久精品电影| 国产一区视频在线看| 午夜免费日韩视频| 一区二区三区国产精品| 国产精品成人午夜| 亚洲一区欧美一区| 欧美成人午夜剧场免费观看| 亚洲欧美高清| 欧美黄色一区二区| 亚洲欧美激情四射在线日| 欧美在线观看一区| 免费在线日韩av| 亚洲香蕉视频| 亚洲女人天堂成人av在线| 久久av老司机精品网站导航| 亚洲欧美日韩国产一区二区| 久久精品国产2020观看福利| 久久久久一区二区三区四区| 亚洲一区免费看| 国产一区二区无遮挡| 校园春色综合网| 国产人久久人人人人爽| 亚洲一级片在线观看| 亚洲黄色在线视频| 欧美精品99| 久久精品国产96久久久香蕉| 久久婷婷久久一区二区三区| 一区二区免费看| 先锋影音久久久| 日韩一级精品| 麻豆成人综合网| 欧美一区二区免费| 国产精品成人国产乱一区| 久久人人超碰| 欧美日韩中文在线观看| 欧美在线视频一区| 欧美电影免费网站| 久久视频在线免费观看| 国产精品草草| 免费永久网站黄欧美| 国产精品视频一| 久久视频一区| 夜夜嗨av一区二区三区中文字幕 | 99国内精品久久| 久久亚洲午夜电影| 久久精品国产91精品亚洲| 欧美私人网站| 99精品热视频| 亚洲特级片在线| 欧美日韩国产精品自在自线| 蜜臀久久99精品久久久久久9 | 99精品免费网| 99国产精品视频免费观看一公开| 久久精品动漫| 欧美成人第一页| 亚洲电影在线看| 另类亚洲自拍| 亚洲精品久久久久| 亚洲看片免费| 欧美性做爰猛烈叫床潮| 亚洲靠逼com| 欧美一区二区三区在线视频| 国产一区二区观看| 欧美69视频| 欧美一区二区三区成人| 男女精品视频| 中文在线不卡视频| 国产目拍亚洲精品99久久精品 | 国产欧美日韩另类一区| 久久精品国产99| 亚洲二区免费| 亚洲欧美日韩综合aⅴ视频| 国内外成人在线视频| 卡通动漫国产精品| 一区二区av在线| 免费日韩av电影| 性久久久久久久久| 99国产精品视频免费观看| 国产一区av在线| 国产精品进线69影院| 午夜精品视频一区| 亚洲精品日韩精品| 你懂的成人av| 久久精品国产第一区二区三区最新章节 | 午夜精品电影| 亚洲精品视频在线播放| 久久久www免费人成黑人精品| 日韩亚洲欧美中文三级| 伊人精品久久久久7777| 欧美亚男人的天堂| 欧美成人午夜激情在线| 久久成人免费网| 欧美有码视频| 香蕉国产精品偷在线观看不卡| 亚洲欧洲日韩综合二区| 麻豆freexxxx性91精品| 亚洲日本一区二区| 欧美呦呦网站| 久久久www成人免费毛片麻豆| 亚洲精品永久免费| 国产精品九色蝌蚪自拍| 欧美在线中文字幕| 欧美精品 国产精品| 极品尤物久久久av免费看| 亚洲欧美一区二区三区极速播放 | 国产一区成人| 亚洲蜜桃精久久久久久久 | 国产在线拍偷自揄拍精品| 亚洲精品久久久久久一区二区 | 国产精品久久久久秋霞鲁丝| 国产亚洲毛片| 亚洲最新合集| 亚洲国产精品久久久久| 亚洲欧美综合国产精品一区| 久久久久九九九九| 国产精品一级在线| 国产综合久久久久久| 日韩视频专区| 久久久久久久久久久一区 | 一区免费在线| 亚洲激情另类| 欧美一级欧美一级在线播放| 欧美大片18| aⅴ色国产欧美| 欧美激情综合色综合啪啪| 国产亚洲一区二区在线观看| 99热精品在线| 亚洲日本在线观看| 欧美精品一区二区三区高清aⅴ| 91久久久精品| 亚洲人体偷拍| 国产精品网站一区| 久久久精品一品道一区| 免费观看在线综合| 亚洲精品视频在线看| 亚洲免费观看视频| 欧美久久成人| 99riav1国产精品视频| 亚洲国产精品久久久久秋霞蜜臀| 久久深夜福利免费观看|