Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <queue>
- using namespace std;
- #include <cstdio>
- #define N 1111
- #define M 111111
- int n,m,ef[M],es[M],first[N],next[M],c,d[N],S,T;
- bool b[N];
- queue<int> q;
- void add(int x,int y){
- next[++c]=first[x];first[x]=c;
- ef[c]=x;es[c]=y;
- }
- void bfs(int x){
- b[x]=1;q.push(x);
- while (!q.empty()){
- int v=q.front();q.pop();
- for (int h=first[v];h;h=next[h])
- if (!b[es[h]]){
- b[es[h]]=1;
- d[es[h]]=d[v]+1;
- q.push(es[h]);
- }
- }
- }
- int main(){
- scanf("%d%d%d%d",&n,&m,&S,&T);
- for (int i=1;i<=m;i++){
- int x,y;
- scanf("%d%d",&x,&y);
- add(x,y);
- }
- bfs(S);
- printf("%d",d[T]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment