Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int bfs(vector<int>v[],int source,int n,int des)
- {
- queue<int>q;
- int level[n+1]={0};
- q.push(source);
- level[source]=1;
- while(!q.empty())
- {
- int u=q.front();;
- q.pop();
- for(int i=0; i<v[u].size(); i++)
- {
- if(level[v[u][i]]==0)
- {
- level[v[u][i]]=level[u]+1;
- q.push(i);
- if(v[u][i]==des)
- {
- return level[des];
- }
- }
- }
- }
- }
- int main()
- {
- int n,e,i,j,n1,n2;
- cin>>n>>e;
- vector<int>v[n+1];
- for(i=0; i<e; i++)
- {
- cin>>n1>>n2;
- v[n1].push_back(n2);
- }
- cout<<bfs(v,1,n,10)<<endl;
- cout<<bfs(v,2,n,8)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement