splash365

dijstra

Aug 30th, 2020 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN = 107;
  5. const int INF = INT_MAX;
  6.  
  7.  
  8. int N, E;
  9. int adj[MAXN][MAXN];
  10. bool vis[MAXN];
  11. int parent[MAXN];
  12. int dist[MAXN];
  13.  
  14. void init()
  15. {
  16.     for(int i=0; i<MAXN; i++)
  17.     {
  18.         dist[i] = INF;
  19.         parent[i] = -1;
  20.     }
  21. }
  22.  
  23. struct Node
  24. {
  25.     int v, distance;
  26.     Node(int v, int distance)
  27.     {
  28.         this->v = v;
  29.         this->distance = distance;
  30.     }
  31. };
  32.  
  33. bool operator < (const Node &a, const Node &b)
  34. {
  35.     if(a.distance > b.distance) return true;
  36.     else return false;
  37. }
  38.  
  39.  
  40. void dijstra(int src)
  41. {
  42.     dist[src] = 0;
  43.     priority_queue<Node> pq;
  44.     pq.push(Node(src, dist[src]));
  45.  
  46.     while(pq.size() != 0)
  47.     {
  48.         int u = pq.top().v;
  49.         pq.pop();
  50.  
  51.         if(vis[u] == true) continue;
  52.  
  53.         vis[u] = true;
  54.  
  55.         for(int v = 0; v<N; v++)
  56.         {
  57.             int cost = adj[u][v];
  58.             if(cost != 0)
  59.             {
  60.                 if(dist[u] + cost < dist[v])
  61.                 {
  62.                     dist[v] = dist[u] + cost;
  63.                     parent[v] = u;
  64.                     pq.push(Node(v, dist[v]));
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. void input()
  72. {
  73.     cin >> N >> E;
  74.     for(int i = 0; i<E; i++)
  75.     {
  76.         int u,v,w;
  77.         cin >> u >> v >> w;
  78.         adj[u][v] = w;
  79.     }
  80. }
  81.  
  82.  
  83. void PrintPath(int x)
  84. {
  85.     if(x == -1) return;
  86.     PrintPath(parent[x]);
  87.     cout << x << " ";
  88. }
  89.  
  90. int main()
  91. {
  92.     freopen("input.txt","r",stdin);
  93.     freopen("output.txt","w",stdout);
  94.  
  95.     init();
  96.     input();
  97.  
  98.     dijstra(0);
  99.  
  100.     for(int i = 0; i<N; ++i)
  101.     {
  102.         cout << "Node " << i << ": " << endl;
  103.         cout << "Path : ";
  104.         PrintPath(i);
  105.         cout << "Distance : " << dist[i] << endl;
  106.         cout << endl;
  107.         cout << endl;
  108.     }
  109. }
  110.  
  111.  
  112. /*
  113. input image: https://www.geeksforgeeks.org/wp-content/uploads/Fig-11.jpg
  114.  
  115. 9 14
  116. 0 1 4
  117. 0 7 8
  118. 1 7 11
  119. 1 2 8
  120. 7 8 7
  121. 7 6 1
  122. 2 8 2
  123. 8 6 6
  124. 2 3 7
  125. 2 5 4
  126. 6 5 2
  127. 3 5 14
  128. 3 4 9
  129. 5 4 10
  130.  
  131. */
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
Add Comment
Please, Sign In to add comment