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

dp_1_M

最近開始刷奇奇神的dp專題,呃,我是弱菜,啥都不會,現在才開始刷dp,
在nocLyt神的熏陶下,覺著區間dp有些感覺了
不過還是調半天才調出來
今天做的這個
M - Optimal Array Multiplication Sequence
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description


 Optimal Array Multiplication Sequence 

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 tex2html_wrap_inline67 array, and B is a tex2html_wrap_inline71 array, it will take tex2html_wrap_inline73 , 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 tex2html_wrap_inline103 array, Y is a tex2html_wrap_inline67 array, and Z is a tex2html_wrap_inline111 array. Let's look at the number of multiplications required to compute the product using the two different sequences:

(X Y) Z

  • tex2html_wrap_inline119 multiplications to determine the product (X Y), a tex2html_wrap_inline123 array.
  • Then tex2html_wrap_inline125 multiplications to determine the final result.
  • Total multiplications: 4500.

X (Y Z)

  • tex2html_wrap_inline133 multiplications to determine the product (Y Z), a tex2html_wrap_inline139 array.
  • Then tex2html_wrap_inline141 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 multiplications 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 tex2html_wrap_inline157 . 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))

雖說這個是水題吧,但好歹是自己用心寫的一個dp,就放在這了,

#include<stdio.h>
#include
<string.h>
#include
<math.h>
#define inf 0x7ffffff
#define maxn 15
int f[maxn][maxn],path[maxn][maxn];
int l[maxn],r[maxn];
int n;
int min(int a,int b)
{
    
return a<b?a:b;
}
void print(int h,int t)
{
    printf(
"(");
    
if (t-h==1)
    {
        printf(
"A%d x A%d",h,t);
    }
    
else
    {
        
int tmp=path[h][t];
        
if(tmp-h==0)
        {
            printf(
"A%d x ",h);
             
if(t-tmp-1==0) printf("A%d",t);else print(tmp+1,t);
        }
        
else if(t-tmp==0)
        {
            
            
if(h-tmp-1==0) printf("A%d",h);else print(h,tmp-1);
            printf(
" x A%d",t);
        }
        
else
        {
            
if(h-tmp==0) printf("A%d",h);else print(h,tmp);
            printf(
" x ");
            
if(t-tmp-1==0) printf("A%d",t);else print(tmp+1,t);
        }
    }
    printf(
")");
}
int main()
{
    
int i,j,k,times;
    times
=0;
    
while(scanf("%d",&n)!=EOF&&n!=0)
    {
        times
++;
        
for(i=1; i<=n; i++) scanf("%d%d",&l[i],&r[i]);
        memset(f,
0,sizeof(f));
        
for(i=1; i<=n-1; i++)
            f[i][i
+1]=l[i]*r[i]*r[i+1];//,printf("%d %d %d\n",i,i+1,f[i][i+1]);
        for(i=n-2; i>=1; i--)
        {
            
for(j=i+2; j<=n; j++)
            {
                f[i][j]
=inf;
                
for(k=i; k<=j; k++)
                    
if(f[i][k]+f[k+1][j]+l[i]*r[k]*r[j]<f[i][j])
                    {
                        
//printf("%d %d %d %d %d %d\n",i,k,j,f[i][k],f[k+1][j],l[i]*r[k]*r[j]);
                        f[i][j]=f[i][k]+f[k+1][j]+l[i]*r[k]*r[j];
                        path[i][j]
=k;
                    }
                
//printf("%d %d %d %d\n",i,j,f[i][j],path[i][j]);
            }
        }
        
//printf("%d\n",f[1][n]);
        printf("Case %d: ",times);
        print(
1,n);
        printf(
"\n");
    }
    
return 0;
}

呃,我是water,那個這個還可以用記憶化dp來實現,不過我沒寫過很好的記憶化搜索,干脆每次都寫的遞推實現
不過效率還好……

posted on 2012-06-02 12:25 jh818012 閱讀(137) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

導航

統計

常用鏈接

留言簿

文章檔案(85)

搜索

最新評論

  • 1.?re: poj1426
  • 我嚓,,輝哥,,居然搜到你的題解了
  • --season
  • 2.?re: poj3083
  • @王私江
    (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
  • --游客
  • 3.?re: poj3414[未登錄]
  • @王私江
    0ms
  • --jh818012
  • 4.?re: poj3414
  • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
  • --王私江
  • 5.?re: poj1426
  • 評論內容較長,點擊標題查看
  • --王私江
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            开心色5月久久精品| 欧美福利专区| 欧美一区二区在线播放| 欧美日韩不卡合集视频| 激情另类综合| 欧美一级大片在线观看| 日韩视频在线一区| 欧美激情一区二区三区高清视频| 永久91嫩草亚洲精品人人| 久久精品亚洲| 欧美亚洲综合另类| 黄色亚洲在线| 蜜臀久久99精品久久久画质超高清| 亚洲欧美一区二区视频| 国产精品美女在线| 亚洲欧美激情四射在线日 | 欧美777四色影视在线| 香蕉成人久久| 国产一二三精品| 久久性天堂网| 久久久欧美精品sm网站| 亚洲国内高清视频| 亚洲韩日在线| 欧美成人亚洲成人日韩成人| 99在线精品观看| 一区二区av在线| 国产一区二区精品| 久久深夜福利| 午夜激情一区| 亚洲欧洲av一区二区| 国产日韩欧美不卡| 欧美成人激情视频| 欧美日韩精品中文字幕| 欧美一区亚洲一区| 久久精品网址| 91久久国产精品91久久性色| 蜜乳av另类精品一区二区| 欧美风情在线观看| 亚洲综合999| 一区二区亚洲| 亚洲成在线观看| 欧美日韩亚洲91| 午夜久久久久久| 久久午夜精品一区二区| 影音先锋欧美精品| 99视频一区| 国产乱码精品| 亚洲精品一区二区三区四区高清| 欧美精品1区2区| 久久久久综合一区二区三区| 久久精品一区二区国产| 亚洲午夜电影在线观看| 免费观看在线综合| 欧美性理论片在线观看片免费| 亚洲精品视频啊美女在线直播| 亚洲在线观看免费| 国产精品久久久久国产a级| 美女诱惑一区| 欧美xxx在线观看| 欧美一区二区三区四区高清| 9i看片成人免费高清| 亚洲国产精品成人精品| 亚洲欧洲综合| 狠狠色丁香久久婷婷综合_中| 欧美黑人多人双交| 黑人一区二区| 一本色道久久综合狠狠躁篇的优点| 在线观看欧美日本| 中文网丁香综合网| 一本大道久久a久久精二百| 亚洲欧美国产制服动漫| 亚洲视频一区二区| 久久久精品一品道一区| 欧美在线日韩精品| 欧美日韩国产另类不卡| 亚洲国产成人在线播放| 国产一区二区三区久久精品| 亚洲永久精品大片| 一本一本a久久| 欧美激情一区在线观看| 毛片av中文字幕一区二区| 国产一区二区精品久久99| 亚洲精品中文字幕有码专区| 99re这里只有精品6| 久久久99爱| 鲁大师成人一区二区三区| 国产精品久久久久婷婷| 国产伦精品一区二区三区免费 | 精品999网站| 午夜视频在线观看一区二区三区 | 亚洲激情av| 欧美有码视频| 欧美二区在线播放| 欧美一区二区三区视频在线 | 国产精品久久久久婷婷| 国产主播在线一区| 中日韩美女免费视频网址在线观看 | 国产精品海角社区在线观看| 久久一区二区三区超碰国产精品| 欧美三级电影一区| 欧美激情成人在线视频| 在线电影一区| 久久电影一区| 久久婷婷国产综合国色天香| 国产亚洲精品aa| 亚洲欧美视频在线观看| 久久久美女艺术照精彩视频福利播放| 国产精品二区在线| 中文在线一区| 一本色道88久久加勒比精品| 欧美日韩在线三级| 99re在线精品| 久久不射网站| 在线精品福利| 久久免费99精品久久久久久| 欧美激情第4页| 亚洲精品一区在线| 欧美午夜国产| 亚洲欧美成人| 亚洲人成人一区二区在线观看| 久久中文字幕导航| 亚洲国产三级网| 在线综合视频| 国产精品三级视频| 99精品视频免费| 性8sex亚洲区入口| 国内精品模特av私拍在线观看| 亚洲天堂成人在线视频| 久久这里只有| 亚洲桃花岛网站| 国产精品综合av一区二区国产馆| 久久精品人人爽| 亚洲国产日韩一区| 久久精品日产第一区二区| 在线日韩中文| 国产精品欧美久久| 久久女同精品一区二区| 亚洲午夜电影在线观看| 久久久久久久久久久一区| 亚洲最新中文字幕| 国产精品一区视频网站| 欧美国产欧美亚洲国产日韩mv天天看完整 | 国产精品久久毛片a| 欧美日韩在线免费观看| 国产日韩欧美不卡在线| 91久久中文字幕| 亚洲自拍偷拍视频| 欧美金8天国| 国产午夜精品在线观看| 亚洲欧洲精品天堂一级| 久久精品亚洲乱码伦伦中文 | 玖玖国产精品视频| 欧美激情网友自拍| 亚洲美洲欧洲综合国产一区| 久久野战av| 久热精品视频在线观看| 亚洲成人自拍视频| 欧美**字幕| 亚洲精选在线观看| 久色婷婷小香蕉久久| 新67194成人永久网站| 黄网站色欧美视频| 欧美日韩播放| 欧美激情国产高清| 久久精品国产免费观看| 亚洲精品国产欧美| 亚洲二区在线观看| 国产精品亚洲成人| 欧美性一二三区| 欧美成人精品在线视频| 老司机午夜精品视频| 欧美一区三区二区在线观看| 久久久国产一区二区三区| 在线视频免费在线观看一区二区| 欧美日韩1区2区| 校园激情久久| 欧美国产欧美亚洲国产日韩mv天天看完整| 国产精品美女久久久久久免费| 国产日韩精品久久久| 亚洲国产视频一区| 欧美深夜影院| 小嫩嫩精品导航| 亚洲国产精品视频一区| 久久在线免费视频| 欧美xxx成人| 卡一卡二国产精品| 激情欧美一区二区三区| 亚洲视频在线看| 美女999久久久精品视频| 欧美在线观看网站| 亚洲女人av| 久久在线免费观看视频| 欧美一区二区| 欧美自拍偷拍午夜视频| 亚洲狠狠丁香婷婷综合久久久| 国产欧美一区二区三区久久| 欧美三级乱码| 欧美三级日本三级少妇99| 欧美色区777第一页| 国内精品嫩模av私拍在线观看 |