Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. int main(){
  2.     d(V, INF);
  3.     d[s] = 0; // distance from source s to s is 0
  4.     queue<int> q;
  5.     q.push(s); // start from source
  6.     while (!q.empty()) {
  7.         int u = q.front();
  8.         q.pop(); // queue: layer by layer!
  9.         for (int j = 0; j < (int)AdjList[u].size(); j++) {
  10.             ii v = AdjList[u][j]; // for each neighbor of u
  11.             if (d[v.first] == INF) {
  12.                 // if v.first is unvisited + reachable
  13.                 d[v.first] = d[u] + 1; // make d[v.first] != INF to flag it
  14.                 q.push(v.first); // enqueue v.first for the next iteration
  15.             }
  16.         }
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement