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

dp_1_M

最近開始刷奇奇神的dp專題,呃,我是弱菜,啥都不會(huì),現(xiàn)在才開始刷dp,
在nocLyt神的熏陶下,覺著區(qū)間dp有些感覺了
不過還是調(diào)半天才調(diào)出來
今天做的這個(gè)
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))

雖說這個(gè)是水題吧,但好歹是自己用心寫的一個(gè)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,那個(gè)這個(gè)還可以用記憶化dp來實(shí)現(xiàn),不過我沒寫過很好的記憶化搜索,干脆每次都寫的遞推實(shí)現(xiàn)
不過效率還好……

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


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


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

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

留言簿

文章檔案(85)

搜索

最新評(píng)論

  • 1.?re: poj1426
  • 我嚓,,輝哥,,居然搜到你的題解了
  • --season
  • 2.?re: poj3083
  • @王私江
    (8+i)&3 相當(dāng)于是 取余3的意思 因?yàn)?3 的 二進(jìn)制是 000011 和(8+i)
  • --游客
  • 3.?re: poj3414[未登錄]
  • @王私江
    0ms
  • --jh818012
  • 4.?re: poj3414
  • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
  • --王私江
  • 5.?re: poj1426
  • 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
  • --王私江
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲五月婷婷| 狠狠久久亚洲欧美| 国产日韩一区二区三区在线播放| 中文网丁香综合网| 亚洲日本成人在线观看| 欧美日韩国产成人精品| 欧美一区二粉嫩精品国产一线天| 欧美自拍偷拍| 在线亚洲自拍| 久久一二三区| 亚洲欧美制服另类日韩| 美国成人直播| 欧美一区二区视频观看视频| 欧美高清在线| 久久色在线观看| 欧美日韩精品二区| 牛夜精品久久久久久久99黑人| 欧美天堂亚洲电影院在线观看| 美女任你摸久久| 国产老女人精品毛片久久| 亚洲精华国产欧美| 在线播放日韩欧美| 亚洲综合三区| 亚洲在线观看视频| 欧美黄污视频| 欧美成人午夜剧场免费观看| 国产亚洲福利一区| 中文亚洲欧美| 一区二区三区色| 欧美成人免费全部| 欧美xxxx在线观看| 激情久久久久久久| 午夜在线一区二区| 亚洲午夜三级在线| 亚洲精品影视| 久久五月激情| 欧美一区二区三区啪啪| 亚洲一区中文| 国产精品麻豆成人av电影艾秋| 亚洲美女区一区| 中文日韩欧美| 国产美女搞久久| 另类av导航| 亚洲尤物影院| av成人手机在线| 亚洲一区bb| 欧美日韩第一区| 欧美国产激情| 亚洲第一网站免费视频| 欧美一区二区三区视频在线 | 久久精品免费观看| 宅男噜噜噜66一区二区66| 久久综合中文字幕| 国产婷婷97碰碰久久人人蜜臀| 亚洲图片激情小说| 一本一本久久a久久精品综合妖精| 免费在线视频一区| 欧美三日本三级三级在线播放| 性久久久久久久| 一级日韩一区在线观看| 欧美另类变人与禽xxxxx| 最新亚洲电影| 99精品免费| 欧美网站在线观看| 这里是久久伊人| 欧美综合二区| 国模精品一区二区三区| 亚洲综合国产精品| 久久综合狠狠| 亚洲精品在线一区二区| 日韩一级网站| 久久一本综合频道| 国产原创一区二区| 久久久久天天天天| 亚洲激情电影在线| 亚洲专区一二三| 国产精品久久久久久久久搜平片| 99国产精品一区| 久久久久久9| 日韩一级成人av| 国产精品一区免费视频| 久久一区二区视频| 日韩一级视频免费观看在线| 午夜精品视频在线| 在线看片成人| 国产精品国产一区二区| 久久高清国产| 亚洲看片免费| 久久综合一区| 亚洲欧美日韩精品久久| 狠狠色综合网| 欧美视频导航| 免播放器亚洲一区| 午夜久久资源| 99pao成人国产永久免费视频| 久久精品夜色噜噜亚洲a∨| 激情综合色综合久久综合| 久久一区二区精品| 夜夜精品视频一区二区| 免费日本视频一区| 亚洲人成艺术| 亚洲国产精品一区二区第一页| 久久www成人_看片免费不卡| 国产精品视频一二| 亚洲图片欧洲图片av| 久久久之久亚州精品露出| 亚洲午夜极品| 欧美无乱码久久久免费午夜一区| 亚洲美女色禁图| 亚洲另类在线视频| 欧美精品一区二区三区在线看午夜| 亚洲天堂免费在线观看视频| 一本一本a久久| 午夜精品久久99蜜桃的功能介绍| 欧美黄色aa电影| 久久久精品欧美丰满| 亚洲福利一区| 久久久在线视频| 激情五月婷婷综合| 性亚洲最疯狂xxxx高清| 免费试看一区| 亚洲一级黄色片| 最新成人av在线| 快射av在线播放一区| 久久精品91| 欧美一级日韩一级| 亚洲自啪免费| 亚洲午夜电影| 亚洲一区二区在线视频| 99re6热只有精品免费观看| 亚洲黄色在线| 亚洲国产精品ⅴa在线观看| 在线观看不卡av| 一区二区在线看| 影音先锋另类| 亚洲大胆av| 亚洲国产精品热久久| 亚洲国产精品黑人久久久| 亚洲国产欧美不卡在线观看| 亚洲国产欧美一区二区三区同亚洲| 在线播放日韩| 亚洲国产美女精品久久久久∴| 亚洲人成毛片在线播放女女| 亚洲精品一区二区三区福利| 99精品福利视频| 中国成人在线视频| 午夜精品福利一区二区蜜股av| 香蕉亚洲视频| 久久亚洲春色中文字幕久久久| 久热精品在线视频| 欧美顶级少妇做爰| 亚洲人成网在线播放| 一区二区三区精品久久久| 一区二区三区精密机械公司| 亚洲一区二区在线免费观看| 午夜在线观看免费一区| 欧美巨乳波霸| 亚洲国产色一区| 9l国产精品久久久久麻豆| 欧美成人精品影院| 欧美福利视频| 在线不卡亚洲| 欧美精品电影在线| 日韩亚洲成人av在线| 久久天天躁狠狠躁夜夜爽蜜月| 亚洲欧美变态国产另类| 欧美一区成人| 裸体素人女欧美日韩| 亚洲国产日韩美| 亚洲午夜久久久| 久久久亚洲午夜电影| 欧美日本视频在线| 国产无遮挡一区二区三区毛片日本| 狠狠色噜噜狠狠狠狠色吗综合| 亚洲人体1000| 欧美一区=区| 亚洲国产成人午夜在线一区 | 欧美sm视频| 国产精品久久久久久久浪潮网站| 韩国欧美一区| 中日韩男男gay无套| 久久精选视频| 亚洲美女一区| 久久精品国产亚洲精品| 欧美日韩免费观看一区二区三区| 国产视频一区在线| 一区二区三欧美| 欧美激情精品久久久久久黑人| 国产综合网站| 亚洲激情在线视频| 亚洲欧美中文另类| 欧美aⅴ99久久黑人专区| 亚洲深爱激情| 欧美成人黄色小视频| 国产日韩欧美亚洲| 亚洲一区二区三区在线看| 欧美激情91| 久久免费一区| 国产视频精品免费播放| 亚洲视频 欧洲视频|