Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define boolean int
- #define false 0
- #define true 1
- #include <stdio.h>
- #include <mm_malloc.h>
- #include <memory.h>
- #include <limits.h>
- #define ZERO 0
- #define MAX_VERTICES 5000
- #define MAX_LENGTH INT_MAX
- typedef enum {
- SUCCESS,
- OUT_OF_MEMORY,
- BAD_NUMBER_VERTICES,
- BAD_NUMBER_EDGES,
- BAD_VERTEX,
- BAD_LENGTH,
- BAD_INPUT
- } ReturnCodes;
- const char* exitMessages[] = {
- "ok",
- "out of memory",
- "bad number of vertices",
- "bad number of edges",
- "bad vertex",
- "bad length",
- "bad number of lines"
- };
- typedef struct _graph Graph;
- struct _graph {
- int vertices;
- int edges;
- int64_t** connectivityTable;
- boolean hasSpanningTree;
- };
- void freeDynamic(Graph* g) {
- if (g) {
- if (g -> connectivityTable) {
- for (int i = 0; i < g -> vertices; i++) {
- if (g -> connectivityTable[i]) {
- free(g -> connectivityTable[i]);
- }
- }
- free(g -> connectivityTable);
- }
- free(g);
- }
- }
- boolean inRangeInt(int value, int lb, int ub) {
- if (lb <= value && value <= ub) {
- return true;
- }
- return false;
- }
- boolean inRangeLong(int64_t value, int64_t lb, int64_t ub) {
- if (lb <= value && value <= ub) {
- return true;
- }
- return false;
- }
- ReturnCodes createGraph(Graph* g, int vertices, int edges) {
- g -> vertices = vertices;
- g -> edges = edges;
- g -> hasSpanningTree = false;
- g -> connectivityTable = (int64_t**)calloc((size_t)vertices, sizeof(int64_t*));
- if (!g -> connectivityTable) {
- return OUT_OF_MEMORY;
- }
- for (int i = 0; i < vertices; i++) {
- g -> connectivityTable[i] = (int64_t*)calloc((size_t)vertices, sizeof(int64_t));
- if (!g -> connectivityTable[i]) {
- return OUT_OF_MEMORY;
- }
- }
- for (int i = 0; i < g -> edges; i++) {
- int verticeFrom, verticeTo;
- int64_t length;
- if (scanf("%d%d%lli", &verticeFrom, &verticeTo, &length) < 3) {
- return BAD_INPUT;
- } else {
- if (!inRangeInt(verticeFrom, 1, g -> vertices) || !inRangeInt(verticeTo, 1, g -> vertices)) {
- return BAD_VERTEX;
- } else if (!inRangeLong(length, 0, MAX_LENGTH)) {
- return BAD_LENGTH;
- }
- }
- g -> connectivityTable[verticeFrom - 1][verticeTo - 1] = length;
- g -> connectivityTable[verticeTo - 1][verticeFrom - 1] = length;
- }
- return SUCCESS;
- }
- ReturnCodes KruskalAlgorithm() {
- int numberOfVertices;
- if (scanf("%d", &numberOfVertices) == 0) {
- return BAD_INPUT;
- }
- int numberOfEdges;
- if (scanf("%d", &numberOfEdges) == 0) {
- return BAD_INPUT;
- }
- if (!inRangeInt(numberOfVertices, ZERO, MAX_VERTICES)) {
- return BAD_NUMBER_VERTICES;
- } else {
- int maxEdges = numberOfVertices * (numberOfVertices + 1) / 2;
- if (!inRangeInt(numberOfEdges, ZERO, maxEdges)) {
- return BAD_NUMBER_EDGES;
- }
- }
- Graph* graph = calloc(1, sizeof(Graph));
- if (!graph) {
- return OUT_OF_MEMORY;
- }
- ReturnCodes creatingGraph;
- if ((creatingGraph = createGraph(graph, numberOfVertices, numberOfEdges)) != SUCCESS) {
- return creatingGraph;
- }
- return SUCCESS;
- }
- int main() {
- ReturnCodes returnCode;
- if ((returnCode = KruskalAlgorithm()) != SUCCESS) {
- printf("%s", exitMessages[returnCode]);
- return returnCode;
- }
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment