GastonFontenla

Untitled

Jul 3rd, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue> ///priority_queue
  4.  
  5. #define INF (1 << 29) ///2^29 * 1 left shift
  6.  
  7. using namespace std;
  8.  
  9. struct Arista ///unidireccional
  10. {
  11.     int nodo;
  12.     int costo;
  13. };
  14.  
  15. Arista armar(int hasta, int costo)
  16. {
  17.     Arista ar;
  18.     ar.nodo = hasta;
  19.     ar.costo = costo;
  20.     return ar;
  21. }
  22.  
  23. struct Grafo
  24. {
  25.     vector <vector <Arista> > adj;
  26.     int nodos, aristas, inicio;
  27.  
  28.     void leer()
  29.     {
  30.         cin >> nodos >> aristas;
  31.  
  32.         adj.resize(nodos+1);
  33.  
  34.         int desde, hasta, costo;
  35.  
  36.         for(int i=0; i<aristas; i++)
  37.         {
  38.             cin >> desde >> hasta >> costo;
  39.             adj[desde].push_back(armar(hasta, costo));
  40.             adj[hasta].push_back(armar(desde, costo));
  41.         }
  42.  
  43.         cin >> inicio;
  44.     }
  45.  
  46.     void Dijkstra()
  47.     {
  48.         vector <int> dist(nodos+1, INF);
  49.         vector <bool> usado(nodos+1, false);
  50.  
  51.         priority_queue <pair<int, int>, vector <pair<int, int>>, greater<pair<int, int>> > pq;
  52.  
  53.         /**
  54.         Metes elementos y siempre sacas el mayor
  55.         Complejidad computacional = O(log(N))
  56.         **/
  57.  
  58.         dist[inicio] = 0;
  59.         pq.push({0, inicio});
  60.  
  61.         for(int i=0; i<nodos; i++)
  62.         {
  63.             pair<int, int> par = pq.top(); /// O(1)
  64.             pq.pop();
  65.  
  66.             int n = par.second;
  67.             usado[n] = true;
  68.  
  69.             for(int j=0; j<adj[n].size(); j++)
  70.             {
  71.                 int vecino = adj[n][j].nodo;
  72.                 int costo = adj[n][j].costo;
  73.                 if(dist[n] + costo < dist[vecino])
  74.                 {
  75.                     dist[vecino] = dist[n] + costo;
  76.                     pq.push({dist[vecino], vecino}); ///E veces, log(n) -> O(E*log(N))
  77.                     ///E -> arista
  78.                     ///E -> N*(N-1)/2 -> cuadrático -> N*N
  79.                     ///O(N*N*log(N)) -> O(N^2*log(N))
  80.                 }
  81.             }
  82.         }
  83.  
  84.         cout << "Distancias desde el inicio hasta cada nodo: " << endl;
  85.  
  86.         for(int i=1; i<dist.size(); i++)
  87.         {
  88.             cout << dist[i] << ", ";
  89.         }
  90.         cout << endl;
  91.  
  92.     }
  93.  
  94. };
  95.  
  96. int main()
  97. {
  98.     Grafo g;
  99.     g.leer();
  100.     g.Dijkstra();
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment