title: Explanation for part of least cost method in c++
body: <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  
  int main(){
  int c[20][20],
  i,j, 
  min, m, n, b, d, c2, c1, p, q;
  int dem[20],sup[20],rf[20],cf[20],sum=0;
  
  
    cout&lt;&lt;"\n number of Row: ";
    cin&gt;&gt;m;
    cout&lt;&lt;"\n number of columns: ";
    cin&gt;&gt;n;
  
  
    cout&lt;&lt;"\n Cost: \n";         //matrix
      for(i=0;i&lt;m;i++)
          {
              for(j=0; j&lt;n; j++)
              {
              cout&lt;&lt;" Cost Matrix : "&lt;&lt;(i+1)&lt;&lt;(j+1);
              cin&gt;&gt;c[i][j];
          }
      }
  
  
    cout&lt;&lt;"\n  Demand: \n";
    for(i=0; i&lt;n; i++){
    cout&lt;&lt;" Demand [ : "&lt;&lt;(i+1);
     cin&gt;&gt;dem[i];
    }
  
    cout&lt;&lt;"\n Supply: \n";
    for(i=0; i&lt;m; i++){ //
    cout&lt;&lt;" Supply  : "&lt;&lt;(i+1);
     cin&gt;&gt;sup[i];
    }
  
    cout&lt;&lt;"\n Matrix:\n";
    for(i=0; i&lt;m; i++){
      for(j=0; j&lt;n; j++)
          cout&lt;&lt;" %d | "&lt;&lt;c[i][j];
          cout&lt;&lt;"%d  "&lt;&lt;sup[i];
          cout&lt;&lt;"\n";
      }
    for(j=0; j&lt;n; j++)
     cout&lt;&lt;"%d  "&lt;&lt;dem[j];
  
  
  
    for(i=0; i&lt;m; i++)
     rf[i]=0;
    for(i=0; i&lt;n; i++)
     cf[i]=0;
    b=m;
    d=n;
  
  
    while(b&gt;0 &amp;&amp; d&gt;0)
    { min=1000;
      for(i=0; i&lt;m; i++)
      { if(rf[i]!=1)
        { for(j=0; j&lt;n; j++)
      { if(cf[j]!=1)
        { if(min&gt;c[i][j])
          { min=c[i][j];
             p=i;
             q=j;
          }
        }
      }
        }
      }
      if(sup[p] &lt; dem[q])
       c1=sup[p];
      else
       c1=dem[q];
  
  
  
      for(i=0; i&lt;m; i++)
      { if(rf[i]!=1)
        { for(j=0; j&lt;n; j++)
      { if(cf[j]!=1)
  
  { if(min==c[i][j])
          { if(sup[i]&lt;dem[j])
             c2=sup[i];
            else
             c2=dem[j];
  
            if(c2&gt;c1)
            { c1=c2;
          p=i;
          q=j;
            }
          }
        }
      }
        }
      }
      //cout&lt;&lt;"\n  %d %d %d ",min,p,q);
      if(sup[p]&lt;dem[q])
      { sum+=c[p][q]*sup[p];
        dem[q]-=sup[p];
        rf[p]=1;
        b--;
      }
      else
      if(sup[p]&gt;dem[q])
      { sum=sum+c[p][q]*dem[q];
        sup[p]-=dem[q];
        cf[q]=1;
        d--;
      }
      else
      if(sup[p]==dem[q])
      { sum=sum+c[p][q]*sup[p];
        rf[p]=1;
        cf[q]=1;
        b--;
        d--;
      }
  
      //cout&lt;&lt;"\n %d",sum);
    }
    cout&lt;&lt;"\n\n total cost %d\n\n"&lt;&lt;sum;
    system("pause");
  }
  </code></pre>
  
  <p>This code is for the least cost method. I don't understand what is the job of <code>rf[i]</code> and <code>cf[i]</code> and why is <code>rf[i]!=1</code></p>
  
  <p>Least Cost Method steps:</p>
  
  <ol>
  <li><p>Determine the least cost among all the rows of the transportation table.</p></li>
  <li><p>Identify the row and allocate the maximum feasible quantity in the cell corresponding to the least cost in the row. Then eliminate that row (column) when an allocation is made.</p></li>
  <li><p>Repeat steps 1 and 2 for the reduced transportation table until all the available quantities are distributed to the required places. If the minimum cost is not unique, the tie can be broken arbitrarily.</p></li>
  </ol>
  
  <p>example in two pictures:
  <a href="http://i.stack.imgur.com/sV9Mb.png" rel="nofollow">first table</a></p>
  
  <p><a href="http://i.stack.imgur.com/ONDHa.png" rel="nofollow">last table</a></p>
  
  <p>The cost of the allocation by the least cost method is 35 x 5 + 35 x 7 + 30 x 4 + 7 x 7 + 43 x 7 = 890</p>