Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- struct ed{
- int to, tout, tin;
- };
- const int inf = 2e9;
- void sh(ed x){
- cout<<x.to<<' '<<x.tout<<' '<<x.tin<<"\n";
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n, the1st, last,r;
- cin>>n>>the1st>>last>>r;
- the1st--; last--;
- vector <vector <ed> > G(n);
- for (int i = 0; i < r; ++i){
- int fr, frt, to, tot;
- cin>>fr>>frt>>to>>tot;
- fr--; to--;
- ed e = {to, frt, tot};
- G[fr].push_back(e);
- }
- /*
- debug
- cout<<"G\n";
- for (int i = 0; i < n; ++i){
- cout<<"v = "<<i<<"\n";
- for (ed e: G[i]) sh(e);
- cout<<"\n";
- }
- cout<<"the1st last = "<<the1st<<' '<<last<<"\n";
- */
- vector <int> T(n,inf);
- T[the1st]=0;
- set <pii> Q;
- Q.insert({0, the1st});
- while (!Q.empty()){
- pii nxt = *Q.begin();
- int v = nxt.second;
- Q.erase(Q.begin());
- for (ed e: G[v]){
- int str_t = e.tout, fin_t = e.tin, u = e.to;
- if (T[u] > fin_t && str_t >= T[v]){
- Q.erase({T[u], u});
- T[u] = fin_t;
- Q.insert({T[u], u});
- }
- }
- }
- if (T[last] == inf){
- cout<<-1;
- exit(0);
- }
- cout<<T[last];
- }
Add Comment
Please, Sign In to add comment