Advertisement
mierotom

BFS QUARTA 29/05

May 29th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. int bfs(int origem, int destino)
  2. {
  3.     int dist[100], vis[100], pai[100];
  4.    
  5.     for(int i = 0; i < 100; i++)
  6.         vis[i] = 0;
  7.        
  8.     queue <int> q;
  9.     q.push(origem);
  10.     dist[origem] = 0;
  11.     vis[origem] = 1;
  12.     pai[origem] = -1;
  13.    
  14.     while(!q.empty())
  15.     {
  16.         int u = q.front();
  17.         q.pop();
  18.        
  19.         for(auto v : adj[u])
  20.         {
  21.             if(!vis[v])
  22.             {
  23.                 q.push(v);
  24.                 vis[v] = 1;
  25.                 dist[v] = dist[u] + 1;
  26.                 pai[v] = u;
  27.             }
  28.         }
  29.     }
  30.    
  31.     return dist[destino];
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement