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

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年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

導航

統計

常用鏈接

留言簿(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久久久久| 欧美夫妇交换俱乐部在线观看| 欧美日韩国产999| 在线日韩av片| 亚洲午夜激情网站| 美女脱光内衣内裤视频久久网站| 亚洲美女av在线播放| 欧美jizz19性欧美| 亚洲视频免费观看| 午夜国产精品视频| 国产午夜精品久久久久久免费视| 久久精品91| 99精品福利视频| 亚洲精品小视频| 亚洲福利视频三区| 欧美a级大片| 久久人体大胆视频| 亚洲激情成人在线| 国产精品久久久久久久一区探花| 性欧美办公室18xxxxhd| 欧美全黄视频| 校园春色综合网| 一区二区冒白浆视频| 激情小说亚洲一区| 欧美成人在线免费视频| 欧美特黄一区| 一区二区亚洲精品国产| 亚洲激情在线播放| 亚洲欧美日韩在线观看a三区| 欧美一区二区三区播放老司机 | 亚洲一区二区三区国产| 欧美影院午夜播放| 嫩草国产精品入口| 亚洲精品乱码久久久久久蜜桃麻豆 | 国产一区二区精品在线观看| 国产日韩欧美在线视频观看| 亚洲精品美女91| 久久久视频精品| 亚洲图片在线观看| 欧美国产专区| 欧美视频手机在线| 国产一区二区三区在线免费观看 | 国产精品制服诱惑| 国产精品亚洲精品| 91久久久久久国产精品| 亚洲欧美国产77777| 欧美成人一区二区三区在线观看 | 欧美日韩一区二区在线观看| 亚洲大胆女人| 欧美一区二区三区播放老司机| 亚洲美女黄网| 欧美激情片在线观看| 国产综合第一页| 久久精品免费| 久久久久久91香蕉国产| 精品1区2区3区4区| 久久综合一区二区| 狼人天天伊人久久| 亚洲精品1区2区| 欧美激情一区在线| 欧美国产第一页| 亚洲美女av网站| 亚洲日本欧美日韩高观看| 欧美国产在线视频| 亚洲综合导航| 欧美一区二区免费| 国产综合av| 亚洲精品国产精品国自产在线| 欧美日韩国产影院| 久久久久久成人| 欧美日韩精品在线视频| 久久久久久久999| 免费黄网站欧美| 欧美一区二区三区免费大片| 欧美在线地址| 欧美国产一区视频在线观看 | 午夜精品久久久久久久99水蜜桃 | 麻豆精品国产91久久久久久| 欧美激情1区2区| 美女啪啪无遮挡免费久久网站| 欧美精品在线一区二区| 免费日韩精品中文字幕视频在线| 欧美色视频日本高清在线观看| 亚洲欧美区自拍先锋| 欧美伦理视频网站| 夜夜嗨av一区二区三区网站四季av | 麻豆精品传媒视频| 亚洲尤物在线| 欧美日韩人人澡狠狠躁视频| 久久综合狠狠综合久久综青草 | 国产精品日韩在线| 日韩天堂av| 亚洲一区二区三区在线观看视频| 久久免费精品视频| 亚洲国产视频一区| 亚洲视频精品| 国产精品实拍| 久久久久女教师免费一区| 欧美国产日韩亚洲一区| 亚洲欧洲一区二区三区在线观看| 欧美成人在线网站| 一本色道久久综合亚洲精品高清 | 欧美激情第10页| 国内一区二区在线视频观看| 麻豆freexxxx性91精品| 欧美国产综合一区二区| 日韩一级黄色大片| 欧美三级电影大全| 欧美午夜不卡视频| 欧美日韩黄色大片| 国产精品免费看久久久香蕉| 国产精品人成在线观看免费 | 亚洲国产天堂网精品网站| 亚洲国产成人av在线| 国产亚洲精品久| 一区免费视频| 国产一区导航| 影音先锋久久资源网| 在线观看日韩国产| 一区二区三区四区五区视频| 亚洲欧美日韩久久精品| 一区二区三区免费观看| 国产精品免费网站| 欧美日韩精品一区二区天天拍小说 | 亚洲精品综合久久中文字幕| 国产精品区免费视频| 国产精品人人做人人爽人人添| 美女国产一区| 欧美福利视频网站| 欧美伦理a级免费电影| 欧美日韩第一区| 国产精品久久午夜| 国产精品国产亚洲精品看不卡15| 欧美日韩亚洲网| 亚洲精品免费看| 日韩小视频在线观看专区| 亚洲欧美精品在线| 亚洲视频中文字幕| 亚洲欧美电影院| 久久国产精品色婷婷| 亚洲综合精品自拍| 午夜国产不卡在线观看视频| 欧美中文在线免费| 欧美中文在线视频| 欧美激情精品久久久久久蜜臀 | 国产一区二区三区网站| 国产日韩亚洲欧美| 在线观看国产一区二区| 日韩视频在线一区二区| 亚洲永久免费观看| 亚洲国产欧美日韩精品| 亚洲狠狠丁香婷婷综合久久久| 亚洲二区视频在线| 亚洲乱码一区二区| 欧美一区二区视频观看视频| 欧美二区不卡| 好看的日韩视频| 亚洲欧美国产日韩天堂区| 快播亚洲色图| 亚洲一区激情| 欧美福利一区二区| 国产麻豆综合| 亚洲欧美日韩在线| 亚洲美女区一区| 欧美剧在线免费观看网站| 亚洲国产mv| 久久av二区| 欧美亚洲在线| 国产一区视频在线看| 亚洲免费av观看| 你懂的视频一区二区| 久久xxxx| 亚洲东热激情| 国产精品video| 一区二区三区产品免费精品久久75| 久久精品视频免费观看| 亚洲午夜久久久久久久久电影院 | 亚洲精品视频在线观看网站| 欧美一区二区观看视频| 亚洲无亚洲人成网站77777| 欧美天天在线| 亚洲欧美色一区| 欧美一区二区性| 亚洲国产精品福利| 亚洲欧洲一区二区天堂久久 | 午夜一区在线| 亚洲精品免费在线观看| 国产亚洲制服色| 艳女tv在线观看国产一区| 亚洲精品一级| 久久久国产精品一区| 欧美在线观看视频| 国产精品高潮呻吟视频| 久久免费观看视频| 国产精品嫩草99av在线| 亚洲高清色综合| 亚洲国产视频一区二区| 欧美亚洲在线观看|