Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- queue<int>q;
- int adj[101][101],d[101],p[101],n,c[101],a,b;
- void bfs(int s) {
- for (int i=1;i<=n;i++) {
- d[i]=1000000000;
- }
- d[s] = 0;
- q.push(s);
- c[s]=1;
- while (!q.empty()) {
- int u = q.front();
- q.pop();
- for (int i=1;i<=n;i++) {
- if (adj[u][i]==1 && c[i]==0) {
- d[i] = d[u] + 1;
- c[i]=1;
- q.push(i);
- p[i]=u;
- }
- }
- c[u]==2;
- }
- }
- int main() {
- cin>>n>>a>>b;
- int ones=0;
- for (int i=1;i<=n;i++)
- for (int j=1;j<=n;j++)
- cin>>adj[i][j];
- bfs(a);
- if (d[b]==1000000000)
- cout<<0<<endl;
- else
- cout<<d[b]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement