Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <algorithm>
- # include <iostream>
- # include <iomanip>
- # include <string>
- # include <cmath>
- # include <deque>
- # include <stack>
- # include <stdio.h>
- # include <set>
- # include <map>
- # include <queue>
- using namespace std;
- const long long INF=1e18;
- set< pair< long long ,long long > > q;
- int main()
- {
- long long m,n,s,f,x,y,t,dis,i,d[200],to,v;
- cin>>n>>s>>f>>m;
- vector< pair< pair< long long , long long >, long long > > a[n+1];
- for(i=1;i<=m;i++)
- {
- cin>>x>>t>>y>>dis;
- a[x].push_back(make_pair(make_pair(y,t),dis));
- }
- for(i=1;i<=n;i++)
- d[i]=INF;
- d[s]=0;
- q.insert(make_pair(s,0));
- while(!q.empty())
- {
- v=q.begin()->first;
- t=q.begin()->second;
- q.erase(q.begin());
- for(i=0;i<a[v].size();i++)
- {
- to=a[v][i].first.first;
- x=a[v][i].first.second;
- y=a[v][i].second;
- if(d[to]>=(abs(x-t)+abs(y-x)+d[v]) && x>=t)
- {
- q.erase(make_pair(to,y));
- d[to]=abs(x-t)+abs(y-x)+d[v];
- q.insert(make_pair(to,y));
- }
- }
- }
- if(d[f]>=INF)
- cout<<"-1"<<endl;
- else cout<<d[f]<<endl;
- return 0;
- }
- /*
- 5
- 1 2
- 7
- 1 2 3 8
- 1 0 2 100
- 1 0 5 9
- 3 8 4 10
- 3 8 5 10
- 5 10 4 13
- 4 10 2 12
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement