#include "dec_menu.c"
int transp(double **c,double **x,int M,int N)
{
 extern char win[5][6];
 extern char Kofp,limit;
 FILE *f;
 int i,j,k,imax,jmax,count;
 double *v,*u;
 u = new double [M+1];
 v = new double [N+1];
 count = 0;
 while(1)
 {
  poten(c,x,v,u,M,N);
  k = imax = jmax = 0;
  for(i=0;i<M;i++)
  for(j=0;j<N;j++)
  if((!x[i][j])&&(v[j]-u[i] > c[i][j])) k = 1;
  if(k)
  {
   for(i=0;i<M;i++)
   for(j=0;j<N;j++)
   if((v[jmax]-u[imax]-c[imax][jmax]) < (v[j]-u[i]-c[i][j]))
   {
    imax = i;
    jmax = j;
   }
  }
  else break;
  if(Kofp)
  {
   f = fopen("tmpfile.tmp","w");
   fputs(" потенциалы:\nu: ",f);
   for(i=0;i<M;i++) fprintf(f,"%.1lf ",u[i]);
   fputs("\nv: ",f);
   for(i=0;i<N;i++) fprintf(f,"%.1lf ",v[i]);
   fputs("\nПромежуточный план :\n",f);
   for(i=0;i<M;i++)
   {
    for(j=0;j<N;j++) if(x[i][j] == -2) fprintf(f,"+0.0 ");
                     else fprintf(f,"%.1lf ",x[i][j]);
    fputs("\n",f);
   }
   fprintf(f," ведущий элемент %d %d \n count = %d \n",imax,jmax,count);
   for(k=i=0;i<M;i++)
   for(j=0;j<N;j++) if(x[i][j]>0) k++;
   if(k < N+M-1) fputs(" Транспортная задача вырождена ",f);
   fclose(f);
   maliar_space(win[2][0]+1,win[2][1]+1,win[2][2]-1,win[2][3]-1,win[2][4]);
   view(win[2][0],win[2][1],win[2][2],win[2][3],win[2][4]);
   maliar_space(win[2][0]+1,win[2][1]+1,win[2][2]-1,win[2][3]-1,win[2][4]);
  }
  x[imax][jmax] = -1.0;
  improvement(x,imax,jmax,M,N);
  count++;
 }
 delete v;
 delete u;
 return count;
}
