Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. # include <algorithm>
  2. # include <iostream>
  3. # include <iomanip>
  4. # include <string>
  5. # include <cmath>
  6. # include <deque>
  7. # include <stack>
  8. # include <stdio.h>
  9. # include <set>
  10. # include <map>
  11. # include <queue>
  12. using namespace std;
  13.  
  14. const long long INF=1e18;
  15. set< pair< long long ,long long > > q;
  16.  
  17. int main()
  18. {
  19. long long m,n,s,f,x,y,t,dis,i,d[200],to,v;
  20. cin>>n>>s>>f>>m;
  21.  
  22. vector< pair< pair< long long , long long >, long long > > a[n+1];
  23.  
  24. for(i=1;i<=m;i++)
  25. {
  26. cin>>x>>t>>y>>dis;
  27. a[x].push_back(make_pair(make_pair(y,t),dis));
  28. }
  29.  
  30. for(i=1;i<=n;i++)
  31. d[i]=INF;
  32. d[s]=0;
  33.  
  34. q.insert(make_pair(s,0));
  35.  
  36. while(!q.empty())
  37. {
  38. v=q.begin()->first;
  39. t=q.begin()->second;
  40. q.erase(q.begin());
  41.  
  42. for(i=0;i<a[v].size();i++)
  43. {
  44. to=a[v][i].first.first;
  45. x=a[v][i].first.second;
  46. y=a[v][i].second;
  47.  
  48. if(d[to]>=(abs(x-t)+abs(y-x)+d[v]) && x>=t)
  49. {
  50. q.erase(make_pair(to,y));
  51. d[to]=abs(x-t)+abs(y-x)+d[v];
  52. q.insert(make_pair(to,y));
  53. }
  54. }
  55. }
  56.  
  57. if(d[f]>=INF)
  58. cout<<"-1"<<endl;
  59. else cout<<d[f]<<endl;
  60.  
  61. return 0;
  62. }
  63. /*
  64. 5
  65. 1 2
  66. 7
  67. 1 2 3 8
  68. 1 0 2 100
  69. 1 0 5 9
  70. 3 8 4 10
  71. 3 8 5 10
  72. 5 10 4 13
  73. 4 10 2 12
  74. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement