Advertisement
matheus_monteiro

Linhas de Ônibus

Sep 18th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int G[580][580];
  5.  
  6. int32_t main() {
  7.     int t, l, o, d;
  8.    
  9.     cin >> t >> l >> o >> d; o--; d--;
  10.    
  11.     while(l--) {
  12.         int c;
  13.         cin >> c;
  14.         vector<int> L(c);
  15.         for(int j = 0; j < c; j++)
  16.             cin >> L[j], L[j]--;
  17.         for(int j = 0; j < c; j++)
  18.             for(int k = j + 1; k < c; k++)
  19.                 G[L[j]][L[k]] = G[L[k]][L[j]] = 1;
  20.     }
  21.    
  22.     queue<int> q;
  23.     vector<int> dist(t + 1, -1);
  24.    
  25.     dist[o] = 0;
  26.     q.push(o);
  27.    
  28.     while(!q.empty()) {
  29.         int u = q.front();
  30.         q.pop();
  31.         for(int w = 0; w < t; w++)
  32.             if(G[u][w] and dist[w] == -1) {
  33.                 dist[w] = dist[u] + 1;
  34.                 q.push(w);
  35.             }
  36.     }
  37.     cout << dist[d] << '\n';
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement