#include "dec_menu.c"
void opor(double **c,double **d,double **x,double *a,double *b)
{
 extern int N,M,s_v;
 extern char win[5][6];
 double **x1,*index1,*index2;
 double s,eps;
 char fl;
 int i,j,imin,jmin;
 index1 = new double[M];
 index2 = new double[N];
 x1 = new double* [M+1];
 for(i=0;i<M+1;i++) x1[i] = new double [N+1];
 for(i=0;i<M;i++)
 {
  index1[i] = a[i];
  for(j=0;j<N;j++)  x[i][j] = -1;
 }
 for(j=0;j<N;j++) index2[j] = b[j];
 imin = jmin = 0;
 while(1)
 {
  for(i=0;i<M;i++)
  {
   if(index1[i])
   for(j=0;j<N;j++) if(x[i][j] == -1 && index2[j])
		   {
		    imin = i;
		    jmin = j;
		    break;
		   }
   if(j<N) break;
  }
  for(i=0;i<M;i++)
  for(j=0;j<N;j++) if( x[i][j] == -1 && c[i][j] <= c[imin][jmin] )
		     {
		      imin = i;
		      jmin = j;
		     }
  x[imin][jmin] = minimum(index1[imin], index2[jmin], d[imin][jmin]);
  index1[imin] -= x[imin][jmin];
  index2[jmin] -= x[imin][jmin];
  if(!index1[imin]) for(j=0;j<N;j++) if(x[imin][j] == -1) x[imin][j] = 0;
  if(!index2[jmin]) for(i=0;i<M;i++) if(x[i][jmin] == -1) x[i][jmin] = 0;
  fl = 0;
  for(i=0;i<M;i++)
  for(j=0;j<N;j++) if(x[i][j] ==  -1) fl = 1;
  if(!fl) break;
 }

 for(i=0;i<M;i++)
 {
  s=0;
  for(j=0;j<N;j++) s += x[i][j];
  x[i][N] = a[i] - s;
 }
 for(j=0;j<N;j++)
 {
  s=0;
  for(i=0;i<M;i++) s += x[i][j];
  x[M][j] = b[j] - s;
 }
 eps = 0;
 for(i=0;i<M;i++) eps += x[i][N];
 if(eps > 0)
 {
  for(i=0;i<M;i++)
  for(j=0;j<N;j++) x1[i][j] = x[i][j];
  a[M] = eps;
  b[N] = eps;
  c[M][N] = x[M][N] = 0;
  for(i=0;i<M;i++) c[i][N] = 1.0e10;
  for(j=0;j<N;j++) c[M][j] = 1.0e10;
  for(i=0;i<M+1;i++) d[i][N] = 1.0e10;
  for(j=0;j<N+1;j++) d[M][j] = 1.0e10;
  for(i=0;i<M;i++) x[i][N] = index1[i];
  for(j=0;j<N;j++) x[M][j] = index2[j];
  M++;
  N++;
  i = transp1(c,x,d,M,N,eps,1);
  if(i==-2) error("Задача неразрешима"); 
  if(i==-3)
  {
   for(i=0;i<M;i++)
   for(j=0;j<N;j++) x[i][j] = x1[i][j];
  }
 M--;
 N--;
 }
 for(i=0;i<M+1;i++) delete x1[i];
 delete x1;
 delete index1;
 delete index2;
}
//**********************************************************
double minimum(double a, double b, double c)
{
 double s;
 s = a;
 if(b < s) s = b;
 if(c < s) s = c;
 return s;
}
