Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <queue>
- using namespace std;
- int main()
- {
- int n, i, j, vBegin, vEnd, x, current;
- scanf("%d", &n);
- vector < vector <int> > list(n);
- for(i = 0; i < n; i++)
- {
- for(j = 0; j < n; j++)
- {
- scanf("%d", &x);
- if(x)
- {
- list[i].push_back(j);
- list[j].push_back(i);
- }
- }
- }
- scanf("%d%d", &vBegin, &vEnd);
- vBegin--, vEnd--;
- queue <int> q;
- q.push(vBegin);
- vector <int> len(n, -1);
- len[vBegin] = 0;
- while(true)
- {
- if(q.empty())
- {
- printf("-1");
- return 0;
- }
- current = q.front();
- q.pop();
- if(current == vEnd)
- {
- printf("%d", len[vEnd]);
- return 0;
- }
- for(int next : list[current])
- {
- if(len[next] == -1)
- {
- q.push(next);
- len[next] = len[current] + 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement