Explicarea algoritmului


Programul C++
#include<fstream.h>
int a[20][20], n, m, k=1;
const infinit = 10000;
 
void citiregraf()
{
  int i,x, y, j, c;
  ifstream f ("graf.in");
  f>>n>>m;
  for(i=1; i<=m; i++)
   { f>>x>>y>>c;
    a[x][y]=a[y][x]=c;
   }
  for (i=1; i<=n; i++)
    for(j=1; j<=n; j++)
      if(i==j) a[i][j]==0;
      else if(a[i][j]==0) a[i][j]=infinit;
  f.close();
}
 
void drum(int i, int j)
{
  int k=1, gasit=0;
  while( (k<=n) && (!gasit) )
    {
      if((i!=k) && (j!=k) && (a[i][j]==a[i][k]+a[k][j]) )
    { drum(i,k); drum(k,j); gasit=1; }
      k++;
    }
  if(!gasit) cout<<j<<" ";
}
void scrie_drum(int s, int f)
{
  if(a[s][f]<infinit)
    {
      cout<<"drumul de la "<<s<<" la "<<f<<" are lungimea "<<a[s][f]<<endl;
      cout<<s<<" ";
      drum(s,f);
      cout<<endl;
    }
  else
    cout<<"nu exista drum de la "<<s<<" la "<<f;
}
 
void alg_royfloyd()
 {
   int i, j, k;
   for(k=1; k<=n; k++)
     for(i=1; i<=n; i++)
       for(j=1; j<=n; j++)
     if (a[i][j]>a[i][k]+a[k][j])
       a[i][j]=a[i][k]+a[k][j];
 
 }
 
void main()
{ int i, s;
  citiregraf();
 
  alg_royfloyd();
  cout<<"dati nodul de start";
  cin>>s;
 
  for(i=1; i<=n; i++)
    scrie_drum(s,i);
}