#include "dec_menu.c"
void poten(double **c,double **x,double *v,double *u,int M,int N)
{
 char *index,stop=0;
 int i,j,n,k,I=0,J=0;
 int *index1,*index2;
 index1 = new int [N+M];
 index2 = new int [N+M];
 if(N > M)
 {
  index = new char [N];
  for(i=0;i<N;i++) index[i] = 0;
 }
 else
 {
  index = new char [M];
  for(i=0;i<M;i++) index[i] = 0;
 }
 for(n=i=0;i<M;i++)
 for(j=0;j<N;j++)
 if(x[i][j])
 {
  index1[n] = i;
  index2[n] = j;
  n++;
 }
 index1[n] = index2[n] = -1;
 u[0] = 0;
 I++;
 for(j=0;j<n;j++) if(!index1[j])
		  {
		   v[index2[j]] = c[index1[j]][index2[j]];
		   index[index2[j]] = 1;
		   index1[j] = index2[j] = -1;
                   J++;
		  }
 for(i=0;stop == 0;i++)
 {
  if((i+1)%2)
  {
   for(j=0;j<N;j++) if((index[j] == 1)||(index[j] == 3))
		   {
                    for(k=0;k<n;k++)
                    if(index2[k] == j)
                    {
                     u[index1[k]] = v[index2[k]] - c[index1[k]][index2[k]];
                     if((index[index1[k]]==1)&&(index1[k] > j)) 
                      index[index1[k]] = 3;
                     else index[index1[k]] = 2;
                     index1[k] = index2[k] = -1;
                     I++;
                    }
                    if(index[j] == 1) index[j] = 0;
                   }
  }
  else
   for(j=0;j<M;j++) if((index[j] == 2)||(index[j] == 3))
		   {
                    for(k=0;k<n;k++)
                    if(index1[k] == j)
                    {
                     v[index2[k]] = c[index1[k]][index2[k]] + u[index1[k]];
                     if((index[index2[k]] == 2)&&(index2[k] > j)) 
                      index[index2[k]] = 3;
                     else index[index2[k]] = 1;
                     index1[k] = index2[k] = -1;
                     J++;
                    }
                    if(index[j] == 2) index[j] = 0;
                   }
  if((J == N)&&(I == M)) stop = 1;
 }
 delete index1;
 delete index2;
 delete index;
}
