a_pramanik

bfs

Apr 12th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,m,i,j,k,l,p,q,x,y,z,src,des;
  6. vector<int>a[100];
  7. scanf("%d%d",&n,&m);
  8. for(i=0;i<m;i++)
  9. {
  10. scanf("%d %d",&p,&q);
  11. a[p].push_back(q);
  12. a[q].push_back(p);
  13. }
  14. scanf("%d%d",&src,&des);
  15. int vis[1000];
  16. memset(vis,-1,sizeof vis);
  17. queue<int>t;
  18. t.push(src);
  19. vis[src]=0;
  20. while(t.size()>0)
  21. {
  22. x=t.front();
  23. t.pop();
  24. for(j=0;j<a[x].size();j++)
  25. {
  26. y=a[x][j];
  27. if(vis[y]==-1)
  28. {
  29. vis[y]=vis[x]+1;
  30. t.push(y);
  31. }
  32. }
  33. }
  34. printf("%d\n",vis[des]);
  35.  
  36. return 0;
  37. }
Add Comment
Please, Sign In to add comment