Advertisement
monyca98

dijkstra

May 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. #include<functional>
  5. #include<set>
  6. #define INF 1000000000
  7. typedef std::vector<int> distances;
  8. typedef std::pair<int, int> p;
  9. typedef std::vector<p> graph;
  10. graph *G;   //here will be stored the graph
  11. distances dist; //here will be stored the distances for every node
  12.  
  13.  
  14. //-----------------dijkstra----------------------------//
  15. void dijkstra(int source, int n)
  16. {
  17.     dist.assign(n, INF);
  18.     dist[source] = 0;
  19.     std::set<p> Q;
  20.     Q.insert({0, source});
  21.     while (!Q.empty())
  22.     {
  23.         auto top = Q.begin();
  24.         int u = top->second;
  25.         Q.erase(top);
  26.         for (auto& e : G[u])
  27.         {
  28.             int v = e.first;
  29.             int w = e.second;
  30.             if (dist[v] > dist[u] + w)
  31.             {
  32.                 if (Q.find({ dist[v],v }) != Q.end())
  33.                     Q.erase(Q.find({ dist[v],v }));
  34.                 dist[v] = dist[u] + w;
  35.                 Q.insert({ dist[v],v });
  36.             }
  37.         }
  38.     }
  39. }
  40. void main_dijkstra()
  41. {
  42.     int n, m, u, v, w, source;
  43.     std::cout << "Give the no of nodes and the no of edges:";
  44.     std::cin >> n >> m;
  45.     G = new graph[n + 1];
  46.     std::cout << "Give the edges(their extremes and the weigh):" << std::endl;
  47.     for (int i = 0; i < m; i++)
  48.     {
  49.         std::cin >> u >> v >> w;
  50.         G[u].push_back({ v,w });
  51.         G[v].push_back({ u,w });
  52.     }
  53.     std::cout << "Give the source:";
  54.     std::cin >> source;
  55.  
  56.  
  57.     dijkstra(source, n);
  58.     for (int i = 0; i < n; i++)
  59.     {
  60.         std::cout << dist[i] << " " << std::endl;
  61.     }
  62. }
  63.  
  64. //---------------optimised dijkstra--------------------//
  65.  
  66. void dijkstra_priority_queue(int source, int N)
  67. {
  68.     //create a priority queue to store the pairs of nodes
  69.     std::priority_queue<p, std::vector<p>, std::greater<p>> Q;
  70.     dist.assign(N, INF);
  71.     dist[source] = 0;
  72.     Q.push({ 0,source });
  73.     while (!Q.empty())
  74.     {
  75.         int a = Q.top().second;
  76.         Q.pop();
  77.         for (auto &e : G[a])
  78.         {
  79.             int b = e.first;
  80.             int c = e.second;
  81.            
  82.             if (dist[b]>dist[a] + c)
  83.             {
  84.                 dist[b] =dist[a]+ c;
  85.                 Q.push({ dist[b],b });
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. void main_dijkstra_priority_queue()
  92. {
  93.     int n, m, u, v, w, source;
  94.     std::cout << "Give the no of nodes and the no of edges:";
  95.     std::cin >> n >> m;
  96.     G = new graph[n + 1];
  97.     std::cout << "Give the edges(their extremes and the weigh):"<<std::endl;
  98.     for (int i = 0; i < m; i++)
  99.     {
  100.         std::cin >> u >> v >> w;
  101.         G[u].push_back({ v,w });
  102.         G[v].push_back({ u,w });
  103.     }
  104.     std::cout << "Give the source:";
  105.     std::cin >> source;
  106.  
  107.    
  108.     dijkstra_priority_queue(source, n);
  109.     for (int i = 0; i < n; i++)
  110.     {
  111.         std::cout << dist[i] << " " << std::endl;
  112.     }
  113. }
  114. //----------------optimised dijkstra--------------------//
  115.  
  116. int main()
  117. {
  118.     std::cout << "----------------------------------dijkstra----------------------" << std::endl;
  119.     main_dijkstra();
  120.  
  121.     std::cout << "---------------------------optimised dijkstra----------------------" << std::endl;
  122.     main_dijkstra_priority_queue();
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement