Image

Programs - C++ - Operation Research

Program:


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class nwrule
{

int a[10][10],s,d,tcost,min,x,y,m,z;
public:
void getdata();
void result();
};
void nwrule::getdata()
{
cout<<"Enter the no. of souces";
cin>>s;
cout<<"Enter the no. of destinations";
cin>>d;
cout<<"Enter the cost of transportation from source to dest.";
for(int i=1;i<=s;i++)
  {
   for(int j=1;j<=d;j++)
   {
cin>>a[i][j];
   }
  }
cout<<"Enter no. of sources available";
for(i=1;i<=s;i++)
  {
   cin>>a[i][d+1];
  }
cout<<"Enter no. of sources available";
for(int j=1;j<=d;j++)
  {
   cin>>a[s+1][j];
  }
  a[s+1][d+1]=0;
}

void nwrule::result()
{
cout<<"Initial Basis"<<endl;
for(int i=1;i<=s+1;i++)
  {
   for(int j=1;j<=d+1;j++)
   {
cout<<setw(3)<<a[i][j];
   }
   cout<<endl;
  }
  tcost=0;
  p:
  min=a[1][1];
cout<<"\n"<<min;
for(i=1;i<=s;i++)
  {
for(int j=1;j<=d ;j++)
 {
   if(min>=a[i][j])
   {
    min=a[i][j];
    x=i;
    y=j;
   }
 }
 }
cout<<"\n i:"<<x;
cout<<"\n j:"<<y;
cout<<"\n min:"<<min;
  i=1;
int j=1;
if(a[x][d+1]>=a[s+1][y])
 {
   tcost+=(a[s+1][y]*min);
   a[x][d+1]-=a[s+1][y];
  for(i=1;i<=s;i++)
  a[i][y]=1000;
  }
 else
 {
     tcost+=(a[x][d+1]*min);
     a[s+1][y]-=a[x][d+1];
     for(j=1;j<=d;j++)
     a[x][j]=1000;
   }
for(i=1;i<=s;i++)
 {
  for(j=1;j<=d;j++)
  {
   if(a[i][j]!=1000)
   goto p;
  }
 }
  cout<<"\n total cost:"<<tcost<<endl;
}
void main()
{
  clrscr();
  nwrule n;
  n.getdata();
  n.result();
  getch();
}

/*output:
10
70
30
40
60
40
8
70
20
Enter no. of supply available
7
9
18
Enter no. of demand available
5
7
8
14
Initial Basis
19 30 50 10 7
70 30 40 60 9
40 8 70 20 18
 5 7 8 14 0

19
i:3
j:2
min:8
19
i:1
j:4
min:10
i:3
j:4
min:20

i:3
j:1
min:40

i:2
j:3
min:40

i:2
j:1
min:70
total cost:816
*/