Advertisement
RaFiN_

Gauss

Feb 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. int n ,tot[MAX],pos[MAX]; double edge[MAX][MAX],res[MAX];vector<pii>v[MAX];
  2.  
  3.  
  4. int gauss(int row,int col)
  5. {
  6.     mem(pos,-1);
  7.     int free_var = 0;
  8.     for(int i = 1,j = 1;i<=row,j<=col;i++,j++)
  9.     {
  10.         int mx = i;
  11.         for(int k = i;k<=row;k++)if(abs(edge[mx][j])<abs(edge[k][j]))  mx = k;
  12.         if(abs(edge[mx][j])>EPS)
  13.         {
  14.             pos[j] = i;
  15.             for(int l = j;l<=col;l++)swap(edge[i][l],edge[mx][l]);
  16.             for(int l = 1;l<=row;l++)
  17.             {
  18.                 if(l==i)continue;
  19.                 double xx = edge[l][j] / edge[i][j];
  20.                 for(int pp = j;pp<=col;pp++)edge[l][pp]-=(edge[i][pp]*xx);
  21.             }
  22.  
  23.         }
  24.     }
  25.  
  26.     for(int l = 1;l<col;l++)
  27.     {
  28.         if(pos[l]==-1)free_var++;
  29.         else res[l] = edge[pos[l]][col]/edge[pos[l]][l];
  30.     }
  31.     for(int i = 1;i<=row;i++)
  32.     {
  33.         double val = 0.0;
  34.         for(int j = 1;j<col;j++)val+=(res[j]*edge[i][j]);
  35.         if(abs(val - edge[i][col])>EPS)return -1;
  36.     }
  37.     return free_var;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement