Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <limits.h>
  5.  
  6. typedef struct heap HEAP;
  7. typedef struct pair PAIR;
  8. typedef struct node NODE;
  9. typedef struct graph GRAPH;
  10.  
  11. struct pair {
  12.     int first; //distance between nodes
  13.     int second; //number of the nodes
  14. };
  15.  
  16. struct heap {
  17.     PAIR data[100000];
  18.     int size;
  19. };
  20.  
  21. struct node {
  22.     PAIR data;
  23.     NODE *next;
  24. };
  25.  
  26. struct graph {
  27.     NODE *vertex[100000];
  28.     bool visited[100000];
  29.     int size;
  30. };
  31.  
  32. void print_list(NODE *head) {
  33.     NODE *current = head;
  34.     while (current != NULL) {
  35.         printf("(%d, %d) -> ", current->data.first, current->data.second);
  36.         current = current->next;
  37.     }
  38.     printf("\n");
  39. }
  40.  
  41. void print_graph(GRAPH *graph) {
  42.     int i;
  43.     for (i = 0; i < graph->size; ++i) {
  44.         printf("Vertice %d: \n", i);
  45.         print_list(graph->vertex[i]);
  46.     }
  47. }
  48.  
  49. void swap(PAIR *x, PAIR *y) {
  50.     PAIR aux = *x;
  51.     *x = *y;
  52.     *y = aux;
  53. }
  54.  
  55. int get_parent_index(HEAP *heap, int i) {
  56.     return i >> 1;
  57. }
  58.  
  59. int get_left_index(HEAP *heap, int i) {
  60.     return i << 1;
  61. }
  62.  
  63. int get_right_index(HEAP *heap, int i) {
  64.     return (i << 1) + 1;
  65. }
  66.  
  67. PAIR item_of(HEAP *heap, int i) {
  68.     return heap->data[i];
  69. }
  70.  
  71. bool is_empty(HEAP *heap) {
  72.     return heap->size == 0;
  73. }
  74.  
  75. GRAPH *create_graph(int number_of_vertex) {
  76.     GRAPH *new_graph = (GRAPH*)malloc(sizeof(GRAPH));
  77.     new_graph->size = number_of_vertex;
  78.     int i;
  79.     for (i = 0; i < number_of_vertex; ++i) {
  80.         new_graph->vertex[i] = NULL;
  81.         new_graph->visited[i] = false;
  82.     }
  83.     return new_graph;
  84. }
  85.  
  86. NODE *create_node(int vertex, int distance) {
  87.     NODE *new_node = (NODE*)malloc(sizeof(NODE));
  88.     PAIR p;
  89.     p.first = distance, p.second = vertex;
  90.     new_node->data = p;
  91.     new_node->next = NULL;
  92.     return new_node;
  93. }
  94.  
  95. HEAP *create_heap(int number_of_vertex) {
  96.     HEAP *new_heap = (HEAP*)malloc(sizeof(HEAP));
  97.     new_heap->size = 0;
  98.     PAIR p;
  99.     int i;
  100.     for (i = 0; i < number_of_vertex; ++i) {
  101.         p.first = -1;
  102.         p.second = -1;
  103.         new_heap->data[i] = p;
  104.     }
  105.     return new_heap;
  106. }
  107.  
  108. void enqueue(HEAP *heap, PAIR item) {
  109.     if (heap->size >= 100000) {
  110.         printf("Heap Overflow! Cannot enqueue!\n");
  111.     } else {
  112.         heap->data[++heap->size] = item;
  113.         int key_index = heap->size;
  114.         int parent_index = get_parent_index(heap, key_index);
  115.         while (parent_index >= 1 && heap->data[key_index].first < heap->data[parent_index].first) {
  116.             swap(&heap->data[key_index], &heap->data[parent_index]);
  117.             key_index = parent_index;
  118.             parent_index = get_parent_index(heap, key_index);
  119.         }
  120.     }
  121. }
  122.  
  123. void min_heapify(HEAP *heap, int i) {
  124.     int smallest;
  125.     int left_index = get_left_index(heap, i);
  126.     int right_index = get_right_index(heap, i);
  127.  
  128.     if (left_index <= heap->size && heap->data[left_index].first < heap->data[i].first) {
  129.         smallest = left_index;
  130.     } else {
  131.         smallest = i;
  132.     }
  133.     if (right_index <= heap->size && heap->data[right_index].first < heap->data[smallest].first) {
  134.         smallest = right_index;
  135.     }
  136.     if (heap->data[i].first != heap->data[smallest].first) {
  137.         swap(&heap->data[i], &heap->data[smallest]);
  138.         min_heapify(heap, smallest);
  139.     }
  140. }
  141.  
  142. PAIR dequeue(HEAP *heap) {
  143.     if (is_empty(heap)) {
  144.         printf("Heap Underflow! Cannot dequeue!\n");
  145.         PAIR p; p.first = -1, p.second = -1;
  146.         return p;
  147.     } else {
  148.         PAIR item = heap->data[1];
  149.         heap->data[1] = heap->data[heap->size];
  150.         --heap->size;
  151.         min_heapify(heap, 1);
  152.         return item;
  153.     }
  154. }
  155.  
  156. void init_visited(GRAPH *graph) {
  157.     int i;
  158.     for (i = 0; i < graph->size; ++i) {
  159.         graph->visited[i] = false;
  160.     }
  161. }
  162.  
  163. NODE *insert_end(NODE *head, int vertex, int distance) {
  164.     if (head == NULL) {
  165.         return create_node(vertex, distance);
  166.     } else {
  167.         NODE *current = head;
  168.         while (current->next != NULL) {
  169.             current = current->next;
  170.         }
  171.         current->next = create_node(vertex, distance);
  172.         return head;
  173.     }
  174. }
  175.  
  176. void add_edge(GRAPH *graph, int from, int to, int distance) {
  177.     graph->vertex[from] = insert_end(graph->vertex[from], to, distance);
  178. }
  179.  
  180. void dijkstra(GRAPH *graph, int source, int *distance) {
  181.     int i = 0;
  182.     for (i = 0; i < graph->size; ++i) {
  183.         distance[i] = INT_MAX;
  184.     }
  185.     distance[source] = 0;
  186.     PAIR p; p.first = 0, p.second = source;
  187.     HEAP *heap = create_heap(graph->size);
  188.     enqueue(heap, p);
  189.     while (!is_empty(heap)) {
  190.         PAIR u = dequeue(heap);
  191.         NODE *current = graph->vertex[u.second];
  192.         while (current != NULL) {
  193.             int actual_dist = distance[u.second] + current->data.first;
  194.             if (actual_dist < distance[current->data.second]) {
  195.                 distance[current->data.second] = actual_dist;
  196.                 p.first = distance[current->data.second], p.second = current->data.second;
  197.                 enqueue(heap, p);
  198.             }
  199.             current = current->next;
  200.         }
  201.     }
  202. }
  203.  
  204. int main() {
  205.     int sz = 1, edges, i;
  206.     scanf("%d %d", &sz, &edges);
  207.     GRAPH *graph = create_graph(sz + 1);
  208.     int distance[sz + 1];
  209.     for (i = 0; i < edges; ++i) {
  210.         int u, v, dist;
  211.         scanf("%d %d %d", &u, &v, &dist);
  212.         add_edge(graph, u, v, dist);
  213.         add_edge(graph, v, u, dist);
  214.     }
  215.     // print_graph(graph);
  216.     dijkstra(graph, 1, distance, predecessors);
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement