Advertisement
xDefo

Scale(risolto)

Nov 7th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int INF = 100000;
  8. const int MAXT = 50000;
  9. const int MAXN = 10000;
  10.  
  11. typedef pair < int, int> nodo;
  12. vector <vector<nodo> > adj;
  13. vector <int> raggiunti[MAXT+1];
  14. bool fatto[MAXN+1];
  15. int dist[MAXN+1];
  16.  
  17. int main()
  18. {
  19.     int N,M;
  20.     freopen("input.txt","r",stdin);
  21.     freopen("output.txt","w",stdout);
  22.     cin>>N;
  23.     cin>>M;
  24.     int A[M];
  25.     int B[M];
  26.     int t1[M];
  27.     int t2[M];
  28.     int temp[M];
  29.     for(int i=0;i<M;i++)
  30.     {
  31.         cin>>A[i];
  32.         cin>>B[i];
  33.         cin>>t1[i];
  34.         cin>>t2[i];
  35.     }
  36.     adj.resize(M);
  37.     for (int i=0;i<M;i++)
  38.     {
  39.         adj[A[i]].push_back(make_pair(i,B[i]));
  40.         adj[B[i]].push_back(make_pair(i,A[i]));
  41.     }
  42.    
  43.     for (int i =0; i<N;i++)
  44.     {
  45.         fatto[i]= false;
  46.         dist[i]=INF;
  47.     }
  48.     raggiunti[0].push_back(0);
  49.     dist[0] = 0;
  50.     for(int t =0;t<=MAXT;t++)
  51.     {
  52.         for(int v : raggiunti[t])
  53.         {
  54.             if(!fatto[v])
  55.             {
  56.                 for(const auto& arco : adj[v])
  57.                 {
  58.                     int scala = arco.first;
  59.                     int sala_raggiunta = arco.second;
  60.                     int tempo = max(dist[v], t1[scala])+1;
  61.                     if(!fatto[sala_raggiunta] & dist[v] < t2[scala] & tempo < dist[sala_raggiunta])
  62.                         {
  63.                             dist[sala_raggiunta]=tempo;
  64.                             raggiunti[tempo].push_back(sala_raggiunta);
  65.                         }
  66.                 }
  67.                 fatto[v] = true;
  68.             }
  69.         }
  70.     }
  71.     int soluzione =(dist[N-1] == INF)? -1 : dist[N-1];
  72.     cout<<soluzione;
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement