Advertisement
wgma

Untitled

Sep 25th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define needforspeed ios::sync_with_stdio(0);cin.tie(0)
  3. #define endl '\n'
  4. #define pb push_back
  5. #define mp make_pair
  6. #define mp3(a,b,c) make_pair(a,make_pair(b,c))
  7. #define mp4(a,b,c,d) make_pair(make_pair(a,b),make_pair(c,d))
  8. #define trace1(a) cout << (a) << endl;
  9. #define trace2(a,b) cout << (a)  << " " << (b) << endl;
  10. #define trace3(a,b,c) cout << (a)  << " " << (b) << " " << (c) << endl;
  11. #define trace4(a,b,c) cout << (a)  << " " << (b) << " " << (c) <<  << (d) << endl;
  12. #define ms(a,b) memset( (a), (b), sizeof(a))
  13. #define fi(x) freopen(x,"r",stdin)
  14. #define fo(x) freopen(x,"w",stdout)
  15. #define all(x) (x).begin(),(x).end()
  16. #define len(x) (int)(x).size()
  17. #define xx first
  18. #define yy second
  19. #define LL long long
  20. #define MAXN (int)10005
  21. #define inf 0x3f3f3f3f
  22. #define DB 0
  23. using namespace std;
  24.  
  25. int N,M;
  26. bool visited[MAXN];
  27. map< pair<int,int>, int>dist;// node,sitance -> occurrences
  28. vector< pair<int,int> >adj[MAXN];// node,distance
  29.  
  30. void dijkstra(int start,int dest){
  31.      set< pair< pair<int,int>,int > >q;// distance, node, occurrences
  32.      q.insert(mp(mp(0,start),1));
  33.      dist[mp(start,0)] = 1;// node,distance -> occurrence
  34.      ms(visited,false);
  35.      visited[start] = true;
  36.      while(!q.empty()){
  37.          pair< pair<int,int>, int> C = *(q.begin());
  38.          swap(C.xx.xx,C.xx.yy);// swap the distance with the node,  we only had this so that the set would prioritize by distance
  39.          C.yy = dist[C.xx];// we update it to have the most recent occurrrence count
  40.          // C = (node,distance,occurrences)
  41.          q.erase(q.begin());
  42.          if(C.xx.xx == dest)return;
  43.          if(DB)trace3(C.xx.xx,C.xx.yy,C.yy);
  44.          for(pair<int,int>&E : adj[C.xx.xx]){
  45.              pair< pair<int,int>,int >NEXT = mp(mp(E.xx,E.yy+C.xx.yy),C.yy);
  46.              if(DB){
  47.                cout << "EDGE ";
  48.                trace3(NEXT.xx.xx,NEXT.xx.yy,NEXT.yy);
  49.              }
  50.              if(dist.count(NEXT.xx))
  51.                 dist[NEXT.xx]+=NEXT.yy;
  52.              else{
  53.                 if(!visited[NEXT.xx.xx]){// the node hasn't been visited
  54.                    if(DB)cout << "VISITED" << endl;
  55.                    visited[NEXT.xx.xx] = true;
  56.                    dist[NEXT.xx] = NEXT.yy;
  57.                    swap(NEXT.xx.xx,NEXT.xx.yy);
  58.                    q.insert(NEXT);
  59.                 }
  60.                 else{// the node has been visisted so we want to limit what we push
  61.                     pair< pair<int,int>,int> D = *(--dist.upper_bound(NEXT.xx));
  62.                     if(DB){
  63.                       cout << "VISITED" << endl;
  64.                       trace3(D.xx.xx,D.xx.yy,D.yy);
  65.                     }
  66.                     if(D.xx.yy > NEXT.xx.xx){
  67.                       visited[NEXT.xx.xx] = true;
  68.                       dist[NEXT.xx] = NEXT.yy;
  69.                       swap(NEXT.xx.xx,NEXT.xx.yy);// swap them so it becomes (distance,node,occurrences) again
  70.                       q.insert(NEXT);
  71.                     }
  72.                 }
  73.              }
  74.          }
  75.  
  76.  
  77.      }
  78.  
  79. }
  80.  
  81. int main() {
  82.   #ifdef DBa
  83.   fi("input.txt");
  84.   #endif
  85.   needforspeed;
  86.   cin >> N >> M;
  87.   for(int i = 0;i < M;i++){
  88.       int u,v,w;
  89.       cin >> u >> v >> w;
  90.       adj[u].pb(mp(v,w));
  91.   }
  92.   int s,t;
  93.   cin >> s >> t;
  94.   dijkstra(s,t);
  95.   if(visited[t]){
  96.      pair< pair<int,int>,int> OUT = *(dist.upper_bound(mp(t,-1)));
  97.      cout << OUT.yy << endl;
  98.   }
  99.   else
  100.       cout << -1 << endl;
  101.   return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement