Advertisement
Stoycho_KK

Dijicstra

Apr 3rd, 2022
1,692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.49 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<limits.h>
  4. #include <stdbool.h>
  5.  
  6. #pragma warning (disable : 4996)
  7.  
  8. #define Amsterdam   0
  9. #define DenHaag     1
  10. #define DenHelder   2
  11. #define Utrecht     3
  12. #define Eindhoven   4
  13. #define Maastricht  5
  14. #define Nijmegen    6
  15. #define Zwolle      7
  16. #define Leeuwarden  8
  17. #define Enschede    9
  18. #define Groningen   10
  19. #define Meppel      11
  20.  
  21. struct Graph {
  22.     int** matrix;
  23.     int size;
  24. };
  25.  
  26. struct pair {
  27.     int first;
  28.     int second;
  29. };
  30.  
  31. struct priorityQueue {
  32.     struct pair* data;
  33.     int size;
  34.     int maxSize;
  35. };
  36.  
  37. struct priorityQueue createPriority(int maxSizeArg) {
  38.     struct priorityQueue toReturn;
  39.  
  40.     toReturn.data = (struct pair*)malloc(maxSizeArg * sizeof(struct pair));
  41.  
  42.     toReturn.size = 0;
  43.     toReturn.maxSize = maxSizeArg;
  44.  
  45.     return toReturn;
  46. }
  47.  
  48. void push(struct priorityQueue* q, int vertex, int dist) {
  49.     if (q->size == q->maxSize)
  50.         return;
  51.  
  52.     int index = -1;
  53.  
  54.     for (int i = 0; i < q->size; i++) {
  55.         if (q->data[i].second > dist) {
  56.             index = i;
  57.             break;
  58.         }
  59.     }
  60.  
  61.     if (index == -1) {
  62.         q->data[q->size].first = vertex;
  63.         q->data[q->size].second = dist;
  64.         q->size++;
  65.         return;
  66.     }
  67.  
  68.     for (int i = index; i < q->size + 1; i++)
  69.         q->data[i + 1] = q->data[i];
  70.  
  71.     q->size++;
  72.  
  73.     q->data[index].first = vertex;
  74.     q->data[index].second = dist;
  75. }
  76.  
  77. struct pair pop(struct priorityQueue* q) {
  78.     // if(q->size == 0)
  79.     //     return struct pair(-1, -1);
  80.  
  81.     struct pair toReturn;
  82.     toReturn.first = q->data[0].first;
  83.     toReturn.second = q->data[0].second;
  84.  
  85.     for (int i = 0; i < q->size - 1; i++)
  86.         q->data[i] = q->data[i + 1];
  87.  
  88.     q->size--;
  89.  
  90.     return toReturn;
  91. }
  92.  
  93. struct Graph createGraph(int dim) {
  94.     struct Graph toReturn;
  95.  
  96.     toReturn.matrix = (int**)(malloc(dim * sizeof(int*)));
  97.  
  98.     for (int i = 0; i < dim; i++)
  99.         toReturn.matrix[i] = (int*)malloc(dim * sizeof(int));
  100.  
  101.     for (int i = 0; i < dim; i++)
  102.         for (int j = 0; j < dim; j++)
  103.             toReturn.matrix[i][j] = -1;
  104.  
  105.  
  106.     toReturn.size = dim;
  107.  
  108.     return toReturn;
  109. }
  110.  
  111. void addEdge(struct Graph* g, int start, int end, int weigth) {
  112.     if (start < 0 || start > g->size || end < 0 || end > g->size)
  113.         return;
  114.  
  115.     g->matrix[start][end] = weigth;
  116.     g->matrix[end][start] = weigth;
  117. }
  118.  
  119. struct Graph createPlayground(struct pair* removePairs, int size) {
  120.     struct Graph playground = createGraph(12);
  121.  
  122.     addEdge(&playground, Amsterdam, DenHaag, 46);
  123.     addEdge(&playground, Amsterdam, DenHelder, 77);
  124.     addEdge(&playground, Amsterdam, Utrecht, 26);
  125.     addEdge(&playground, DenHaag, Eindhoven, 89);
  126.     addEdge(&playground, Eindhoven, Maastricht, 63);
  127.     addEdge(&playground, Eindhoven, Nijmegen, 55);
  128.     addEdge(&playground, Eindhoven, Utrecht, 47);
  129.     addEdge(&playground, Enschede, Zwolle, 50);
  130.     addEdge(&playground, Groningen, Leeuwarden, 34);
  131.     addEdge(&playground, Groningen, Meppel, 49);
  132.     addEdge(&playground, Leeuwarden, Meppel, 40);
  133.     addEdge(&playground, Maastricht, Nijmegen, 111);
  134.     addEdge(&playground, Meppel, Zwolle, 15);
  135.     addEdge(&playground, Nijmegen, Zwolle, 77);
  136.     addEdge(&playground, Utrecht, Zwolle, 51);
  137.  
  138.     for (int i = 0; i < size; i++)
  139.         addEdge(&playground, removePairs[i].first, removePairs[i].second, -1);
  140.  
  141.     return playground;
  142. }
  143.  
  144. void freeGraph(struct Graph* g) {
  145.     for (int i = 0; i < g->size; i++)
  146.         free(g->matrix[i]);
  147.  
  148.     free(g->matrix);
  149. }
  150.  
  151. void freeQueue(struct priorityQueue* q) {
  152.     free(q->data);
  153. }
  154.  
  155. bool isEmpty(struct priorityQueue* q) {
  156.     return (q->size == 0);
  157. }
  158.  
  159. int Dijkstra(struct Graph* g, int startVertex, int endVertex) {
  160.     if (startVertex < 0 || startVertex > g->size)
  161.         return INT_MAX;
  162.  
  163.     int* distances = (int*)malloc(g->size * sizeof(int));
  164.  
  165.     for (int i = 0; i < g->size; i++) {
  166.         distances[i] = INT_MAX;
  167.     }
  168.  
  169.     struct priorityQueue q = createPriority(25);
  170.  
  171.     distances[startVertex] = 0;
  172.  
  173.     push(&q, startVertex, 0);
  174.  
  175.     while (!isEmpty(&q)) {
  176.         struct pair minimumNode = pop(&q);
  177.  
  178.         int currentVertex = minimumNode.first;
  179.         int distFromStart = minimumNode.second;
  180.  
  181.         for (int i = 0; i < g->size; i++) {
  182.             if (g->matrix[currentVertex][i] >= 0) {
  183.                 if (distances[i] > g->matrix[currentVertex][i] + distances[currentVertex]) {
  184.                     distances[i] = g->matrix[currentVertex][i] + distances[currentVertex];
  185.                     push(&q, i, distances[currentVertex]);
  186.                 }
  187.             }
  188.         }
  189.     }
  190.  
  191.     freeQueue(&q);
  192.  
  193.     int res = distances[endVertex];
  194.     free(distances);
  195.     return res;
  196. }
  197.  
  198. int main() {
  199.     int numberOfTowns = 0;
  200.     scanf("%d", &numberOfTowns);
  201.  
  202.     struct pair* towns = (struct pair*)malloc(numberOfTowns * sizeof(struct pair));
  203.  
  204.     for (int i = 0; i < numberOfTowns; i++) {
  205.         int fst = 0;
  206.         int snd = 0;
  207.  
  208.         scanf("%d%d", &fst, &snd);
  209.         towns[i].first = fst;
  210.         towns[i].second = snd;
  211.     }
  212.  
  213.     struct Graph g = createPlayground(towns, numberOfTowns);
  214.  
  215.     bool running = true;
  216.  
  217.     while (running) {
  218.         int cmd = 0;
  219.  
  220.         scanf("%d", &cmd);
  221.  
  222.         if (cmd == -1)
  223.             running = false;
  224.        
  225.         int cmdSnd = 0;
  226.         scanf("%d", &cmdSnd);
  227.  
  228.         int res = Dijkstra(&g, cmd, cmdSnd);
  229.  
  230.         printf("%d", res);
  231.     }
  232.  
  233.     freeGraph(&g);
  234.     free(towns);
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement