示例
(n=6)
(P=<5,10,6,20,2,25,30>)
- (A_{1}:5 imes 10)
- (A_{2}:10 imes 6)
- (A_{3}:6 imes 20)
- (A_{4}:20 imes 2)
- (A_{5}:2 imes 25)
- (A_{6}:25 imes 30)
(m[i,j])表示计算(A[i,j])所需的最少运算次数,(s[i,j])表示计算(A[i,j])的最少运算次数是最后一次运算的位置
(Step1)
当(r=1):
(m[1,1]=0), (m[2,2]=0), (m[3,3]=0), (m[4,4]=0), (m[5,5]=0), (m[6,6]=0).
(Step2)
当(r=2),那么需要枚举的 (iin (1,2,3,4,5)) , (jin (2,3,4,5,6))
- (m[1,2]=5 imes 10 imes 6=300)
- (m[2,3]=10 imes 6 imes 20=1200)
- (m[3,4]=6 imes 20 imes 2=240)
- (m[4,5]=20 imes 2 imes 25=1000)
- (m[5,6]=2 imes 25 imes 30=1500)
(Step3)
当(r=3),那么需要枚举的 (iin (1,2,3,4)) , (jin (3,4,5,6))
- (m[1,3]=min(m[1,2]+m[3,3]+P_{0} imes P_{2} imes P_{3},m[1,1]+m[2,3]+P_{0} imes P_{1} imes P_{3}=min(300+600,1200+1000)=900), (s[1,3]=1)
- (m[2,4]=min(m[2,3]+m[4,4]+P_{1} imes P_{3} imes P_{4},m[2,2]+m[3,4]+P_{1} imes P_{2} imes P_{4})=min(1200+400,240+120)=360), (s[2,4]=2)
- (m[3,5]=min(m[3,4]+m[5,5]+P_{2} imes P_{4} imes P_{5},m[3,3]+m[4,5]+P_{2} imes P_{3} imes P_{5})=min(240+300,1000+3000)=540), (s[3,5]=4)
- (m[4,6]=min(m[4,5]+m[6,6]+P_{3} imes P_{5} imes P_{6},m[4,4]+m[5,6]+P_{3} imes P_{4} imes P{6})=min(1000+1500,1500+1200)=2500), (s[4,6]=5)
(Step4)
当(r=4),那么需要枚举的 (iin (1,2,3)) , (jin (4,5,6))
- (m[1,4]=min(m[1,3]+m[4,4]+P_{0} imes P_{3} imes P_{4},m[1,2]+m[3,4]+P_{0} imes P_{2} imes P_{4},m[1,1]+m[2,4]+P_{0} imes P_{1} imes P_{4})=min(900+200,300+240+60,360+100)=460), (s[1,4]=1)
- (m[2,5]=min(m[2,4]+m[5,5]+P_{1} imes P_{4} imes P_{5},m[2,3]+m[4,5]+P_{1} imes P_{3} imes P_{5},m[2,2]+m[3,5]+P_{1} imes P_{2} imes P_{5})=min(360+500,2200+5000,540+1500)=860), (s[2,5]=4)
- (m[3,6]=min(m[3,5]+m[6,6]+P_{2} imes P_{5} imes P_{6},m[3,4]+m[5,6]+P_{2} imes P_{4} imes P_{6},m[3,3]+m[4,6]+P_{2} imes P_{3} imes P_{6})=min(540+4500,1740+300,2500+3600)=2040), (s[3,6]=4)
(Step5)
当(r=5),那么需要枚举的 (iin (1,2)) , (jin (5,6))
- (m[1,5]=min(m[1,4]+m[5,5]+P_{0} imes P_{4} imes P_{5},m[1,3]+m[4,5]+P_{0} imes P_{3} imes P_{5},m[1,2]+m[3,5]+P_{0} imes P_{2} imes P_{5},m[1,1]+m[2,5]+P_{0} imes P_{1} imes P_{5})=min(460+250,900+1000+2500,300+540+750,860+1250)=710), (s[1,5]=4)
- (m[2,6]=min(m[2,5]+m[6,6]+P_{1} imes P_{5} imes P_{6},m[2,4]+m[5,6]+P_{1} imes P_{4} imes P_{6},m[2,3]+m[4,6]+P_{1} imes P_{3} imes P_{6},m[2,2]+m[3,6]+P_{1} imes P_{2} imes P_{6})=min(860+7500,360+1500+600,1200+1200+6000,2040+1800)=2460), (s[2,6]=4)
(Step6)
当(r=6),那么需要枚举的 (iin (1)) , (jin (6))
- (m[1,6]=min(m[1,5]+m[6,6]+P_{0} imes P_{5} imes P_{6},m[1,4]+m[5,6]+P_{0} imes P_{4} imes P_{6},m[1,3]+m[4,6]+P_{0} imes P_{3} imes P_{6},m[1,2]+m[3,6]+P_{0} imes P_{2} imes P_{6},m[1,1]+m[2,6]+P_{0} imes P_{1} imes P_{6})=min(710+3750,460+1500+300,900+2500+3000,300+2040+900,2460+1500)=2260), (s[1,6]=4)
(Step7)
- (ans=2260)
- 运算顺序:
((1)、) (s[1,6]=4 ightarrow ((A_{1} imes A_{2} imes A_{3} imes A_{4}) imes (A_{5} imes A_{6})))
((2)、) (s[1,4]=1 ightarrow ((A_{1} imes (A_{2} imes A_{3} imes A_{4})) imes (A_{5} imes A_{6})))
((3)、) (s[2,4]=2 ightarrow ((A_{1} imes (A_{2} imes (A_{3} imes A_{4}))) imes (A_{5} imes A_{6})))