assembly line Scheduling:
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intMain ()7 {8 inte1,e2,x1,x2,a1[7],a2[7];9 intf[2][7],fe,le,l[2][7],t1[7],t2[7];Tenscanf"%d%d",&e1,&E2); Onescanf"%d%d",&x1,&x2); A for(intI=1; i<=6; i++) -scanf"%d",&a1[i]); - for(intI=1; i<=6; i++) thescanf"%d",&a2[i]); - for(intI=1; i<=5; i++) -scanf"%d",&t1[i]); - for(intI=1; i<=5; i++) +scanf"%d",&t2[i]); -f[1][1]=e1+a1[1]; +f[2][1]=e2+a2[1]; A at for(intj=2; j<=6; j + +) - { - if(f[1][j-1]+a1[j]<=f[2][j-1]+t2[j-1]+A1[j]) - { -f[1][j]=f[1][j-1]+A1[j]; -l[1][j]=1; in } - Else to { +f[1][j]=f[2][j-1]+t2[j-1]+A1[j]; -l[1][j]=2; the } * $ if(f[2][j-1]+a2[j]<=f[1][j-1]+t1[j-1]+A2[j])Panax Notoginseng { -f[2][j]=f[2][j-1]+A2[j]; thel[2][j]=2; + } A Else the { +f[2][j]=f[1][j-1]+t1[j-1]+A2[j]; -l[2][j]=1; $ } $ } - if(f[1][6]+x1<=f[2][6]+x2) - { thefe=f[1][6]+X1; -le=1;Wuyi } the Else - { Wufe=f[2][6]+x2; -le=2; About } $ -printf"%d\n", fe); - - intI=le; Aprintf"Print line%d,station%d\n"I6); + for(intj=6; j>=2; j--) the { -I=L[i][j]; $printf"Print line%d,station%d\n", i,j-1); the } the return 0; the}
Source code Implementation
Matrix chain Multiplication
1#include <iostream>2#include <cstdio>3 #defineINF 1e94 using namespacestd;5 ints[7][7];6 voidPrint_optimal_parens (intIintj)7 {8 if(i==j)9printf"a%d", i);Ten Else One { Aprintf"("); - Print_optimal_parens (I,s[i][j]); -Print_optimal_parens (s[i][j]+1, j); theprintf")"); - } - - } + - intMain () + { A intp[7]; at for(intI=0; i<=6; i++) -scanf"%d",&p[i]); - - intm[7][7]; - for(intI=1; i<=6; i++) -m[i][i]=0; in for(intL=2; l<=6; l++) - { to for(intI=1; i<=6-l+1; i++) + { - intj=i+l-1; them[i][j]=inf; * for(intk=i;k<=j-1; k++) $ {Panax Notoginseng intq=m[i][k]+m[k+1][j]+p[i-1]*p[k]*P[j]; - if(q<M[i][j]) the { +m[i][j]=Q; As[i][j]=K; the } + } - } $ } $printf"%d\n", m[1][6]); - -Print_optimal_parens (1,6); the return 0; -}
View Code
Dynamic programming of Algorithm introduction