Esempio n. 1
0
File: floyd.c Progetto: guluuu3/spoj
// driver program to test above function
int main()
{
    /* Let us create the following weighted graph
            10
       (0)------->(3)
        |         /|\
      5 |          |
        |          | 1
       \|/         |
       (1)------->(2)
            3           */
    int graph[V][V] = { {0,   3,  INF, INF},
                        {3, 0,   4, INF},
                        {INF, 4, 0,   5},
                        {INF, INF, 5, 0}
                      };

    // Print the solution
    floydWarshell(graph);
    return 0;
}
Esempio n. 2
0
// driver program to test above function
int main()
{
    int i=0,j=0;
    char line[100], member[100];
    //int graph[V][V];
	int **graph;
	int shortest_i=-1,shortest_j=-1, shortest_len=INF;

    fp = fopen("g1.txt", "r");
    if (fp == NULL)
           exit(1);

    fgets(line, 100, fp); // Read the first line
	printf("%s", line);
	for (i = 0, j = 0; i < 100; i++) {
		if(line[i] == ' ' || line[i] == '\0') {
			member[j] = '\0';
			j = 0;
			if(num_vertices == -1)
				num_vertices = atoi(member);
			else if (num_edges == -1)
				num_edges = atoi(member);
			else {
				printf("Error in num_Vertices line parsing %d\n", num_vertices);
				exit(-1);
			}
			if(line[i] == '\0')
				break;
		}
		else {
			member[j] = line[i];
			j++;
		}
    }
	printf("Number of vertices: %d\n", num_vertices);

	graph = (int**) malloc(num_vertices*(sizeof(int*)));
	if(!graph) {
        printf("Cannot alloc memory..Exiting\n");
        exit(-1);
    }
	for (i = 0; i < num_vertices; i++){
		graph[i] = (int *) malloc(num_vertices*sizeof(int));
		if(!graph[i]) {
	        printf("Cannot alloc memory..Exiting\n");
    	    exit(-1);
    	}
	}

	for(i = 0; i < num_vertices; i++)
        for(j = 0; j < num_vertices; j++) {
            if ( i == j)
                graph[i][j] = 0;
            else
                graph[i][j] = INF;
    }

	//printmatrix(graph);

    while (!feof(fp)) {
    	// Read file line by line
        fgets(line, 100, fp);
        // Line is of the form vertex1 vertex2 edge-cost
		int vertex1=-1, vertex2=-1, edge=-1;
        for (i = 0, j = 0; i < 100; i++) {
			if(line[i] == ' ' || line[i] == '\0') {
				member[j] = '\0';
				j = 0;
				if(vertex1 == -1)
					vertex1 = atoi(member);
				else if(vertex2 == -1)
					vertex2 = atoi(member);
				else if(edge == -1)
					edge = atoi(member);
				else {
					printf("Error in line parsing\n");
					exit(-1);
				}
				if(line[i] == '\0')
					break;
			}
			else {
				member[j] = line[i];
				j++;
			}
		}
		// Set entry in Graph
		//printf("vertex1: %d Vertex2: %d edge: %d\n", vertex1, vertex2, edge);
		graph[vertex1 -1][vertex2 - 1] = edge;
    }

    // compute
    floydWarshell(graph);

	// Detect if negative cycles
	for (i = 0; i < num_vertices; i++) {
		if(graph[i][i] < 0) {
			printf("Negative cycle exists\n");
			exit(0);
		}
	}

	// Print soln
	for (i = 0; i < num_vertices; i++) {
		for (j = 0; j < num_vertices; j++) {
			if(graph[i][j] < shortest_len) {
				shortest_i = i;
				shortest_j = j;
				shortest_len = graph[i][j];
			}
		}
	}
	printf("Shortest path is from %d to %d with len %d\n", (shortest_i+1),(shortest_j+1), shortest_len);
    return 0;
}