Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <climits>
- using namespace std;
- int main(){
- int p[] = {30,35,15,5,10,20,25};
- int size = 6;
- int ks[6][6];
- int m[6][6];
- for(int i=0;i<6;i++){
- ks[i][i]=0;
- m[i][i]=0;
- }
- for(int i=0; i<6; i++){
- for(int j=i+1; j<6; j++){
- ks[j][i]=-1;
- m[j][i]=-1;
- }
- }
- for(int d=1; d<size; d++){
- int i = 0;
- int j = d;
- while(i<size && j < size){
- int min = INT_MAX;
- int kElegido = i;
- for(int k=i; k<j; k++){
- //Regla es por la indexación
- int costK=m[i][k]+m[k+1][j]+p[i]*p[k+1]*p[j+1];
- if(costK<min){
- min = costK;
- kElegido = k;
- }
- }
- m[i][j] = min;
- ks[i][j] = kElegido+1;
- i++;
- j++;
- }
- }
- cout << "Matriz de costos" << endl;
- for(int i=0; i<size; i++){
- for(int j=0; j<size; j++){
- cout << m[i][j] <<" , " ;
- }
- cout << endl;
- }
- cout << endl << "Matriz de ks" << endl;
- for(int i=0; i<size; i++){
- for(int j=0; j<size; j++){
- cout << ks[i][j] <<" , ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement