Advertisement
jzh4n

Dijkstra - BFS

May 27th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <stack>
  4.  
  5. int num_of_vertex, *map, (*table)[2];
  6. bool *visited_vertex;
  7.  
  8. void dijkstra(int start, int end) {
  9.     std::queue<int> q;
  10.     std::stack<int> path;
  11.  
  12.     q.push(start);
  13.     visited_vertex[0] = true;
  14.  
  15.     while(!q.empty()) {
  16.         int vertex = q.front();
  17.         q.pop();
  18.         visited_vertex[vertex] = true;
  19.  
  20.         for(int i = 0; i < num_of_vertex; ++i) {
  21.             if(map[vertex * num_of_vertex + i] > 0 and visited_vertex[i] == false) {
  22.                 if(table[i][0] == -1 or
  23.                   (table[vertex][0] + map[vertex * num_of_vertex + i] < table[i][0])) {
  24.                       table[i][0] = table[vertex][0] + map[vertex * num_of_vertex + i];
  25.                       table[i][1] = vertex;
  26.                   }
  27.                
  28.                 q.push(i);
  29.             }
  30.         }
  31.     }
  32.  
  33.     for(int i = end;;i = table[i][1]) {
  34.         if(i == -1) return;
  35.  
  36.         path.push(i);
  37.  
  38.         if(table[i][0] == 0) break;
  39.     }
  40.  
  41.     std::cout << "Minumum cost: " << table[end][0] << "\n";
  42.     std::cout << "Path: ";
  43.     while(true) {
  44.         std::cout << path.top();
  45.  
  46.         path.pop();
  47.  
  48.         if(path.empty()) break;
  49.         else std::cout << " -> ";
  50.     }
  51. }
  52.  
  53. int main() {
  54.     int num_of_edge, start_vertex, end_vertex;
  55.    
  56.     std::cin >> num_of_vertex;
  57.     std::cin >> num_of_edge;
  58.     std::cin >> start_vertex;
  59.     std::cin >> end_vertex;
  60.  
  61.     map = new int[num_of_vertex * num_of_vertex];
  62.  
  63.     int n = num_of_vertex * num_of_vertex;
  64.     for(int i = 0; i < n; ++i) map[i] = 0;
  65.  
  66.     visited_vertex = new bool[num_of_vertex];
  67.     for(int i = 0; i < num_of_vertex; ++i)
  68.         visited_vertex[i] = false;
  69.  
  70.     table = new int[num_of_vertex][2];
  71.     for(int i = 0; i < num_of_vertex; ++i) {
  72.         for(int j = 0; j < 2; ++j) {
  73.             table[i][j] = -1;
  74.         }
  75.     }
  76.  
  77.     table[start_vertex][0] = 0;
  78.  
  79.     //generate adjacency matrix
  80.     int source, destination, weight;
  81.     for(int i = 0; i < num_of_edge; ++i) {
  82.         std::cin >> source;
  83.         std::cin >> destination;
  84.         std::cin >> weight;
  85.  
  86.         map[source + destination * num_of_vertex] = weight;
  87.         map[destination + source * num_of_vertex] = weight;
  88.     }
  89.  
  90.     std::cout << "\n";
  91.  
  92.     dijkstra(start_vertex, end_vertex);
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement