Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- using namespace std;
- int main()
- {
- int n;
- cin>>n;
- vector<vector<int> >gm(n+1, vector<int>(n+1));
- for(int i = 1; i <= n; i++)
- {
- for(int j = 1; j <= n; j++)
- {
- int x;
- cin>>x;
- gm[i][j] = x;
- }
- }
- vector<vector<int> >g(n+1);
- for(int i = 1; i <= n; i++)
- {
- for(int j = 1; j <= n; j++)
- {
- if(gm[i][j] == 1)
- {
- g[i].push_back(j);
- }
- }
- }
- int s, k;
- cin>>s>>k;
- int undef = -1;
- vector<int>dist(n+1, undef);
- queue<int>q;
- q.push(s);
- dist[s] = 0;
- while(!q.empty())
- {
- int x = q.front();
- q.pop();
- for(auto u: g[x])
- {
- if(dist[u] == undef)
- {
- dist[u] = dist[x] + 1;
- q.push(u);
- }
- }
- }
- cout<<dist[k];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement