Guest User

Untitled

a guest
Jan 4th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. #define boolean int
  2. #define false 0
  3. #define true 1
  4.  
  5. #include <stdio.h>
  6. #include <mm_malloc.h>
  7. #include <memory.h>
  8. #include <limits.h>
  9.  
  10. #define ZERO 0
  11. #define MAX_VERTICES 5000
  12. #define MAX_LENGTH INT_MAX
  13.  
  14. typedef enum {
  15.     SUCCESS,
  16.     OUT_OF_MEMORY,
  17.     BAD_NUMBER_VERTICES,
  18.     BAD_NUMBER_EDGES,
  19.     BAD_VERTEX,
  20.     BAD_LENGTH,
  21.     BAD_INPUT
  22. } ReturnCodes;
  23.  
  24. const char* exitMessages[] = {
  25.         "ok",
  26.         "out of memory",
  27.         "bad number of vertices",
  28.         "bad number of edges",
  29.         "bad vertex",
  30.         "bad length",
  31.         "bad number of lines"
  32. };
  33.  
  34. typedef struct _graph Graph;
  35.  
  36. struct _graph {
  37.     int vertices;
  38.     int edges;
  39.     int64_t** connectivityTable;
  40.     boolean hasSpanningTree;
  41. };
  42.  
  43. void freeDynamic(Graph* g) {
  44.     if (g) {
  45.         if (g -> connectivityTable) {
  46.             for (int i = 0; i < g -> vertices; i++) {
  47.                 if (g -> connectivityTable[i]) {
  48.                     free(g -> connectivityTable[i]);
  49.                 }
  50.             }
  51.             free(g -> connectivityTable);
  52.         }
  53.         free(g);
  54.     }
  55. }
  56.  
  57. boolean inRangeInt(int value, int lb, int ub) {
  58.     if (lb <= value && value <= ub) {
  59.         return true;
  60.     }
  61.  
  62.     return false;
  63. }
  64.  
  65. boolean inRangeLong(int64_t value, int64_t lb, int64_t ub) {
  66.     if (lb <= value && value <= ub) {
  67.         return true;
  68.     }
  69.  
  70.     return false;
  71. }
  72.  
  73. ReturnCodes createGraph(Graph* g, int vertices, int edges) {
  74.     g -> vertices = vertices;
  75.     g -> edges = edges;
  76.     g -> hasSpanningTree = false;
  77.     g -> connectivityTable = (int64_t**)calloc((size_t)vertices, sizeof(int64_t*));
  78.     if (!g -> connectivityTable) {
  79.         return OUT_OF_MEMORY;
  80.     }
  81.  
  82.     for (int i = 0; i < vertices; i++) {
  83.         g -> connectivityTable[i] = (int64_t*)calloc((size_t)vertices, sizeof(int64_t));
  84.         if (!g -> connectivityTable[i]) {
  85.             return OUT_OF_MEMORY;
  86.         }
  87.     }
  88.  
  89.     for (int i = 0; i < g -> edges; i++) {
  90.         int verticeFrom, verticeTo;
  91.         int64_t length;
  92.  
  93.         if (scanf("%d%d%lli", &verticeFrom, &verticeTo, &length) < 3) {
  94.             return BAD_INPUT;
  95.         } else {
  96.             if (!inRangeInt(verticeFrom, 1, g -> vertices) || !inRangeInt(verticeTo, 1, g -> vertices)) {
  97.                 return BAD_VERTEX;
  98.             } else if (!inRangeLong(length, 0, MAX_LENGTH)) {
  99.                 return BAD_LENGTH;
  100.             }
  101.         }
  102.  
  103.         g -> connectivityTable[verticeFrom - 1][verticeTo - 1] = length;
  104.         g -> connectivityTable[verticeTo - 1][verticeFrom - 1] = length;
  105.     }
  106.  
  107.     return SUCCESS;
  108. }
  109.  
  110. ReturnCodes KruskalAlgorithm() {
  111.     int numberOfVertices;
  112.     if (scanf("%d", &numberOfVertices) == 0) {
  113.         return BAD_INPUT;
  114.     }
  115.  
  116.     int numberOfEdges;
  117.     if (scanf("%d", &numberOfEdges) == 0) {
  118.         return BAD_INPUT;
  119.     }
  120.  
  121.     if (!inRangeInt(numberOfVertices, ZERO, MAX_VERTICES)) {
  122.         return BAD_NUMBER_VERTICES;
  123.     } else {
  124.         int maxEdges = numberOfVertices * (numberOfVertices + 1) / 2;
  125.         if (!inRangeInt(numberOfEdges, ZERO, maxEdges)) {
  126.             return BAD_NUMBER_EDGES;
  127.         }
  128.     }
  129.  
  130.     Graph* graph = calloc(1, sizeof(Graph));
  131.     if (!graph) {
  132.         return OUT_OF_MEMORY;
  133.     }
  134.  
  135.     ReturnCodes creatingGraph;
  136.     if ((creatingGraph = createGraph(graph, numberOfVertices, numberOfEdges)) != SUCCESS) {
  137.         return creatingGraph;
  138.     }
  139.  
  140.     return SUCCESS;
  141. }
  142.  
  143. int main() {
  144.     ReturnCodes returnCode;
  145.     if ((returnCode = KruskalAlgorithm()) != SUCCESS) {
  146.         printf("%s", exitMessages[returnCode]);
  147.         return returnCode;
  148.     }
  149.     return SUCCESS;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment