Advertisement
999ms

Untitled

Mar 1st, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. vector<int> g[n];
  2. int depth[n];
  3. void bfs(int from){
  4. fill(depth,depth+n, inf);
  5. depth[0] = 0;
  6. queue<pair<int,int> > q;
  7. q.push({from,0});
  8. while(q.size()){
  9. auto [nxtv, nxtw] = q.front();
  10. q.pop();
  11. if(depth[nxtv] < nxtw) continue;
  12. for(int v : g[nxtv]){
  13. if(depth[nxtv] + 1 < depth[v]){
  14. depth[v] = depth[nxtv] + 1;
  15. q.push({v,depth[v]});
  16. }
  17. }
  18. }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement