Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Practical no.

9
Aim:- Write an algorithm, draw a flowchart and develop a C++ program to find initial basic
feasible solution of transportation problem using least cost method.
#include<iostream.h>
#include<conio.h>
class lcm
{
int X[10][10],m,n,S[10],D[10],i,j,k,l,p,q,tot1,tot2,TC,temp,pt,sum;
public:
voidgetdata()
{
cout<<"Enter the order of matrix in terms of row and column:";
cin>>m>>n;
cout<<"enter the values row by row: \n";
for(i=1;i<=m;i++)
{
cout<<"Row "<<i;
for(j=1;j<=n;j++)
{
cin>>X[i][j];
}
}
cout<<"enter the values of demand:";
tot1=0;
for(i=1;i<=n;i++)
{
cin>>D[i];
tot1=tot1+D[i];
}
cout<<"Enter the values of supply:";
tot2=0;
for(i=1;i<=m;i++)
{
cin>>S[i];
tot2=tot2+S[i];
}
}
void calculate()
{
cout<<"\n dest'n ";
for(i=1;i<=n;i++)
cout<<i<<" ";
cout<<"Supply";
cout<<"\n source \n";
for(i=1;i<=m;i++)
{
cout<<" "<<i<<" ";
for(j=1;j<=n;j++)

{
cout<<" "<<X[i][j];
}
cout<<" "<<S[i]<<"\n";
}
cout<<"Demand ";
for(i=1;i<=n;i++)
cout<<" "<<D[i];
if(tot1!=tot2)
{
if(tot1<tot2)
{
n=n+1;
for(i=1;i<=m;i++)
X[i][n]=0;
D[n]=tot2-tot1;
}
if(tot1>tot2)
{
m=m+1;
for(i=1;i<=n;i++)
X[m][i]=0;
S[m]=tot1-tot2;
}
}
TC=0;
p=1;
q=1;
sum=1;
again:
while(sum!=0)
{
i=p;
j=q;
pt=X[i][j];
for(k=p;k<=m;k++)
{
for(l=q;l<=n;l++)
{
if(X[k][l]<X[i][j])
{
pt=X[k][l];
i=k;
j=l;
}
}
}
if(D[j]<S[i])
{
temp=pt*D[j];
TC=TC+temp;
S[i]=S[i]-D[j];
D[j]=0;
if(j==n)
{
n=n-1;
}
else
{
q=j;
q=q+1;
}
goto again;
}
if(D[j]>S[i])
{
temp=pt*S[i];
TC=TC+temp;
D[j]=D[j]-S[i];
S[i]=0;
if(i==m)
{
m=m-1;
}
else
{
p=i;
p=p+1;
}
goto again;
}
if(D[j]=S[i])
{
temp=pt*D[j];
TC=TC+temp;
S[i]=0;
D[j]=0;
p=i;
q=j;
p=p+1;
q=q+1;
sum=0;
goto again;
}
}
cout<<"\n\n TC= "<<TC;
}
};
void main()
{
lcm o;
clrscr();
o.getdata();
o.calculate();
getch();
}

Output:-

You might also like