Alex_tz307

1930. Ivan's Car - Timus

Nov 4th, 2020
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. class heap {
  7.     public:
  8.         int cost, node, last;
  9.  
  10.         bool operator < (const heap &A) const {
  11.             return A.cost < cost;
  12.         }
  13. };
  14.  
  15. int main() {
  16.     ios_base::sync_with_stdio(false);
  17.     cin.tie(nullptr);
  18.     cout.tie(nullptr);
  19.     int N, M;
  20.     cin >> N >> M;
  21.     vector < vector < pair < int , int > > > G(N + 1);
  22.     while(M--) {
  23.         int u, v;
  24.         cin >> u >> v;
  25.         G[u].emplace_back(v, 0);
  26.         G[v].emplace_back(u, 1);
  27.     }
  28.     int start, stop;
  29.     cin >> start >> stop;
  30.     priority_queue < heap > Q;
  31.     vector < int > d(N + 1, INF);
  32.     d[start] = 0;
  33.     Q.push(heap{0, start, -1});
  34.     while(!Q.empty()) {
  35.         int node = Q.top().node,
  36.             last = Q.top().last;
  37.         Q.pop();
  38.         for(auto next : G[node]) {
  39.             int C = 1;
  40.             if(last == next.second || last == -1)
  41.                 C = 0;
  42.             if(d[next.first] > d[node] + C) {
  43.                 d[next.first] = d[node] + C;
  44.                 Q.push(heap{d[next.first], next.first, next.second});
  45.             }
  46.         }
  47.     }
  48.     cout << d[stop];
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment