Guest User

Untitled

a guest
Aug 20th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <vector>
  2. #include <set>
  3. #include <climits>
  4. #include <iostream>
  5. #define MAXN 2000005
  6. using namespace std;
  7. vector<pair<int,pair<int,int>>> graph[MAXN];
  8. int mind[MAXN];
  9. int esplora(int N){
  10.     set<pair<int,int>> coda;
  11.     coda.insert(make_pair(0,0));
  12.     mind[0]=0;
  13.     while(!coda.empty()){
  14.         //cout<<"\nEHI";
  15.         int where=coda.begin()->second;
  16.         int time=coda.begin()->first;
  17.         if(where==N-1) return time;
  18.         coda.erase(coda.begin());
  19.         for(auto ed:graph[where]){
  20.             //cout<<" OK ";
  21.             if(time<ed.second.second){
  22.             ////cout<<"1 ";
  23.                 if(time>ed.second.first){
  24.                   //cout<<mind[where]+1<<" ";
  25.                     if(mind[ed.first]>mind[where]+1){
  26.                         coda.erase({mind[ed.first],ed.first});
  27.                         mind[ed.first]=mind[where]+1;
  28.                         coda.insert({mind[ed.first],ed.first});
  29.                     }
  30.                 }
  31.                 else{
  32.                     //cout<<ed.second.first<<" ";
  33.                     if(mind[ed.first]>ed.second.first+1){
  34.                         ////cout<<"4 ";
  35.                         coda.erase({mind[ed.first],ed.first});
  36.                         mind[ed.first]=ed.second.first+1;
  37.                         coda.insert({mind[ed.first],ed.first});
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     //cout<<endl<<endl;
  44.     return -1;
  45. }
  46.  
  47.  
  48. int raggiungi(int N, int M, int A[], int B[], int inizio[], int fine[]) {
  49.     for(int i=0;i<N;i++){
  50.         mind[i]=INT_MAX;
  51.     }
  52.     for(int i=0;i<M;i++){
  53.         graph[A[i]].push_back(make_pair(B[i],make_pair(inizio[i],fine[i])));
  54.         graph[B[i]].push_back(make_pair(A[i],make_pair(inizio[i],fine[i])));
  55.     }
  56.     return esplora(N);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment