Korotkodul

Задача №170. Автобусы OK

Jun 9th, 2022 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17.     for (auto x: v) cout<<x<<' ';
  18.     cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22.     for (auto x: v) cout<<x<<' ';
  23.     cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28.     for (auto x: v) cv(x);
  29.     cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33.     for (bool x: v) cout<<x<<' ';
  34.     cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string>  v){
  38.     for (auto a: v){
  39.         cout<<a<<"\n";
  40.     }
  41. }
  42.  
  43. struct ed{
  44.     int to, tout, tin;
  45. };
  46.  
  47. const int inf = 2e9;
  48.  
  49. void sh(ed x){
  50.     cout<<x.to<<' '<<x.tout<<' '<<x.tin<<"\n";
  51. }
  52.  
  53. int main()
  54. {
  55.     ios::sync_with_stdio(0);
  56.     cin.tie(0);
  57.     cout.tie(0);
  58.     int n, the1st, last,r;
  59.     cin>>n>>the1st>>last>>r;
  60.     the1st--; last--;
  61.     vector <vector <ed> > G(n);
  62.     for (int i = 0; i < r; ++i){
  63.         int fr, frt, to, tot;
  64.         cin>>fr>>frt>>to>>tot;
  65.         fr--; to--;
  66.         ed e = {to, frt, tot};
  67.         G[fr].push_back(e);
  68.     }
  69.     /*
  70.     debug
  71.     cout<<"G\n";
  72.     for (int i = 0; i < n; ++i){
  73.         cout<<"v = "<<i<<"\n";
  74.         for (ed e: G[i]) sh(e);
  75.         cout<<"\n";
  76.     }
  77.     cout<<"the1st last = "<<the1st<<' '<<last<<"\n";
  78.     */
  79.  
  80.     vector <int> T(n,inf);
  81.     T[the1st]=0;
  82.     set <pii> Q;
  83.     Q.insert({0, the1st});
  84.     while (!Q.empty()){
  85.         pii nxt = *Q.begin();
  86.         int v = nxt.second;
  87.         Q.erase(Q.begin());
  88.         for (ed e: G[v]){
  89.             int str_t = e.tout, fin_t = e.tin, u = e.to;
  90.             if (T[u] > fin_t && str_t >= T[v]){
  91.                 Q.erase({T[u], u});
  92.                 T[u] = fin_t;
  93.                 Q.insert({T[u], u});
  94.             }
  95.         }
  96.     }
  97.     if (T[last] == inf){
  98.         cout<<-1;
  99.         exit(0);
  100.     }
  101.     cout<<T[last];
  102. }
  103.  
Add Comment
Please, Sign In to add comment