Advertisement
yeskendir_sultanov

acmp - 127

Jun 11th, 2025
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, g[101][101], d[101], s, f;
  6. bool used[101];
  7. queue <int> q;
  8.  
  9. int main() {
  10.     cin >> n;
  11.     for (int i = 1; i <= n; ++i) {
  12.         for (int j = 1; j <= n; ++j) {
  13.             cin >> g[i][j];
  14.         }
  15.     }
  16.     cin >> s >> f;
  17.    
  18.     used[s] = true;
  19.     d[s] = 0;
  20.     q.push(s);
  21.    
  22.     while (!q.empty()) {
  23.         int v = q.front();
  24.         q.pop();
  25.         for (int to = 1; to <= n; ++to) {
  26.             if (g[v][to] == 1 && !used[to]) {
  27.                 used[to] = true;
  28.                 d[to] = d[v] + 1;
  29.                 q.push(to);
  30.             }
  31.         }
  32.     }
  33.    
  34.     if (!used[f]) {
  35.         cout << -1;
  36.     } else {
  37.         cout << d[f];
  38.     }
  39.    
  40.     return 0;
  41. }  
  42.  
  43.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement