bruh1214

dijkstra

Apr 7th, 2022 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. /*
  2. in.txt
  3. 8
  4. 1 2 4
  5. 1 4 5
  6. 4 5 7
  7. 2 6 2
  8. 6 7 3
  9. 6 8 1
  10. */
  11.  
  12. #include <iostream>
  13. #include <fstream>
  14.  
  15. #define INTS_MAX -1
  16. #define MAX_INT 99
  17.  
  18. using namespace std;
  19. ifstream f("in.txt");
  20.  
  21.  
  22. void initialiseGraph(int graph[][MAX_INT], int nodes) {
  23.     for (int i = 1; i <= nodes; i++) {
  24.         for (int j = 1; j <= nodes; j++) {
  25.             graph[i][j] = INTS_MAX;
  26.         }
  27.     }
  28. }
  29.  
  30. void readGraph(int graph[][MAX_INT], int edges) {
  31.     for (int i = 1; i <= edges; i++) {
  32.         int x, y, w;
  33.         f >> x >> y >> w;
  34.         graph[x][y] = w;
  35.     }
  36. }
  37.  
  38. void showGraph(int graph[][MAX_INT], int nodes) {
  39.     for (int i = 1; i <= nodes; i++) {
  40.         for (int j = 1; j <= nodes; j++) {
  41.             cout << graph[i][j] << " ";
  42.         }
  43.         cout << endl;
  44.     }
  45. }
  46.  
  47. void initiate(int nodes, int shortest_paths[], int start) {
  48.     for (int node = 1; node <= nodes; node++) {
  49.         shortest_paths[node] = INTS_MAX;
  50.     }
  51.     shortest_paths[start] = 0;
  52. }
  53.  
  54. void relax(int graph[][MAX_INT], bool unvisited[], int shortest_path[], int nodes, int current_node) {
  55.     for (int node = 1; node <= nodes; node++) {
  56.         int w = graph[current_node][node];
  57.         if ((w != INTS_MAX && unvisited[node]) &&
  58.             (shortest_path[node] == INTS_MAX || shortest_path[node] > w + shortest_path[current_node])) {
  59.             shortest_path[node] = w + shortest_path[current_node];
  60.         }
  61.     }
  62. }
  63.  
  64. int extractMin(int shortest_path[], bool unvisited[], int nodes) {
  65.     int mins = INTS_MAX;
  66.     int minNode = INTS_MAX;
  67.     for (int node = 1; node <= nodes; node++) {
  68.         if ((shortest_path[node] != INTS_MAX && unvisited[node]) && (mins == INTS_MAX || mins > shortest_path[node])) {
  69.             mins = shortest_path[node];
  70.             minNode = node;
  71.         }
  72.     }
  73.     return minNode;
  74. }
  75.  
  76. void Dijkstra(int graph[][MAX_INT], int source_node, int nodes) {
  77.     int shortest_path[MAX_INT];
  78.     initiate(nodes, shortest_path, source_node);
  79.  
  80.     int hasToVisit = nodes;
  81.     bool unvisited[MAX_INT];
  82.     for (int node = 1; node <= nodes; node++) {
  83.         unvisited[node] = true;
  84.     }
  85.     while (hasToVisit != 1) {
  86.         int currentNode = extractMin(shortest_path, unvisited, nodes);
  87.         unvisited[currentNode] = false;
  88.         hasToVisit--;
  89.         relax(graph, unvisited, shortest_path, nodes, currentNode);
  90.     }
  91.  
  92.     for (int node = 1; node <= nodes; node++) {
  93.         if (shortest_path[node] == INTS_MAX) {
  94.             cout << "INF" << '\t';
  95.         } else {
  96.             cout << shortest_path[node] <<'\t';
  97.         }
  98.     }
  99. }
  100.  
  101. int main() {
  102.     int nodes, edges, weight;
  103.     f >> nodes >> edges >> weight;
  104.     int graph[MAX_INT][MAX_INT];
  105.     initialiseGraph(graph, nodes);
  106.     readGraph(graph, edges);
  107.     showGraph(graph, nodes);
  108.     int source_node = 1;
  109.     Dijkstra(graph, source_node, nodes);
  110.     return 0;
  111. }
Add Comment
Please, Sign In to add comment