Advertisement
Josif_tepe

Untitled

Oct 12th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <queue>
  5. using namespace std;
  6. bool visited[3005][3005];
  7. int main() {
  8.     int n, m;
  9.     cin >> n >> m;
  10.    
  11.     vector<int> graph[n + 5];
  12.     map<vector<int>, bool> pat;
  13.     for(int i = 0; i < m; i++) {
  14.         int a, b;
  15.         cin >> a >> b;
  16.         graph[a].push_back(b);
  17.         graph[b].push_back(a);
  18.     }
  19.     int t;
  20.     cin >> t;
  21.     for(int i = 0; i < t; i++) {
  22.         int a, b, c;
  23.         cin >> a >> b >> c;
  24.         vector<int> v{a, b, c};// v.push_back(a); v.push_back(b); v.push_back(c);
  25.         pat[v] = true;
  26.     }
  27.     queue<int> q; // FIFO --> first in, first out
  28.     q.push(0); // current node
  29.     q.push(-1); // previous node
  30.     q.push(0); // distance from 0 to current node
  31.    
  32.     while(!q.empty()) {
  33.         int current_node = q.front();
  34.         q.pop();
  35.         int previous_node = q.front();
  36.         q.pop();
  37.         int distance = q.front();
  38.         q.pop();
  39.         if(current_node == n - 1) {
  40.             cout << distance << endl;
  41.             return 0;
  42.         }
  43.         for(int i = 0; i < graph[current_node].size(); i++) {
  44.             int sosed = graph[current_node][i];
  45.             if(!visited[current_node][sosed] and !pat[{previous_node, current_node, sosed}]) {
  46.                 q.push(sosed);
  47.                 q.push(current_node);
  48.                 q.push(distance + 1);
  49.                 visited[current_node][sosed] = true;
  50.             }
  51.         }
  52.     }
  53.    
  54.     cout << "GRESHKA" << endl;
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement