示例#1
0
/* Read the user's input and call the search. */
static int input_and_search(FILE * infile, struct node nodes[],
			    unsigned int nnodes)
{
    int err = 0;
    unsigned int s, t;
    unsigned int cost = 0;
    double start, end;

    while (fscanf(infile, "%u %u", &s, &t) == 2) {
	s = s - 1; /* avoiding 1-indexing */
	t = t - 1;
	if (s >= nnodes) {
	    fprintf(stderr, "Start node is invalid\n");
	    continue;
	}
	if (t >= nnodes) {
	    fprintf(stderr, "Target node is invalid\n");
	    continue;
	}
	printf("finding a route from %d to %d\n", s, t);
	start = get_current_seconds();
	err = djikstra(stdout, nodes, nnodes, s, t, &cost);
	end = get_current_seconds();
	if (err)
	    break;
	printf("cost: %u\n", cost);
	printf("time: %f seconds\n", end - start);
    }

    return err;
}
示例#2
0
void main()
{
	int a[10][10],j,i,v;
	printf("Enter vertices");
	scanf("%d %d",&v);
	printf("Enter the adjacency Matrix");
	for(i=0;i<v;i++)
		for(j=0;j<v;j++)
			scanf("%d",&a[i][j]);
	djikstra(a,v);
}
示例#3
0
int main(int argc, char *argv[]){
	
	int graph[N][N] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
                      {4, 0, 8, 0, 0, 0, 0, 11, 0},
                      {0, 8, 0, 7, 0, 4, 0, 0, 2},
                      {0, 0, 7, 0, 9, 14, 0, 0, 0},
                      {0, 0, 0, 9, 0, 10, 0, 0, 0},
                      {0, 0, 4, 0, 10, 0, 2, 0, 0},
                      {0, 0, 0, 14, 0, 2, 0, 1, 6},
                      {8, 11, 0, 0, 0, 0, 1, 0, 7},
                      {0, 0, 2, 0, 0, 0, 6, 7, 0}
                     };
 
    djikstra(graph, 0);

	return 0;
}
示例#4
0
//Função que calcula o caminho minimo entre todos os pares, utilizando djikstra
void minimoTodos(grafo *g, mapa m){
     int countA=0, countB=0, countC=0, countD=0;
     int matrizExc[g->nvertices][g->nvertices];
     int maxExc[g->nvertices];
     int menorExc=0;
     
     for (countA=0, countA < g->nvertices, countA++){
         for(countB=0, countB < g->nvertices, countB++){
                       matrizExc[countA][countB] = djikstra(g,m,countA,countB);
         }
     }
     
     for (countC=0, countC < g->nvertices, countC++){
         for (countD=0, countD < g->nvertices, countD++){
             
             }
         }
}
示例#5
0
文件: djikstra.c 项目: limoges/school
int main(int argc, char ** argv)
{
    char filename[] = "matrix";
    FILE * file = fopen(filename, "r");
    if (file == NULL)
    {
        printf("Couldn't open \'%s\'.", filename);
        perror("fopen");
        exit(EXIT_FAILURE);
    }
	
    int start, end, x, y, size = countl(file);
    int ** matrix = malloc(size * sizeof(int *));
    for (x = 0; x < size; x++)
    {
        matrix[x] = malloc(size * sizeof(int));
        for (y = 0; y < size; y++)
            matrix[x][y] = 0;
    }
	
    char line[BUFLEN];
    int weight, result;
    while (getline(line, BUFLEN, file))
    {
        getnexti(line, ':', &x);
        do
        {
            result = getnexti(NULL, '.', &y);
            if (!result) break;
            result = getnexti(NULL, ';', &weight);
            matrix[x-1][y-1] = weight;
        }
        while (result);
    }
    fclose(file);

    djikstra(matrix, size, 0, 5);
    exit(EXIT_SUCCESS);
}
示例#6
0
文件: main.c 项目: hhismans/lem_in
int		endmain(int error, t_node *list, int nbrofants)
{
	t_pathlist		*pathlist;
	t_path			*tmp;

	if (error == FALSE)
	{
		ft_putendl_fd("ERROR", 2);
		return (0);
	}
	pathlist = NULL;
	while ((tmp = djikstra(list)))
		pathlist = pathlist_pushfront(&pathlist, tmp);
	if (pathlist)
	{
		ants(pathlist, nbrofants);
	}
	else
	{
		ft_putendl_fd("ERROR", 2);
		return (0);
	}
	return (0);
}
int main(int argc, char *argv[]){

	int index = 1, m, tag, rank, size, i, j, aux_i, aux_j, aux_graph, final_sp;
	MPI_Status status;

	// Leitura do número de vértices e arestas
	scanf(" %d %d", &n, &m);

	MPI_Init(&argc, &argv);

	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);

	// Broadcast do número de vértices para todos os nós para a alocação da matriz
	MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

	// Verificão necessária caso o usuário extrapole o número de processos permitido para um determinado número de vértices
	if(size > n){
		if(rank == 0)
			printf("Erro! Total de processos não pode ser maior que o total de vértices!\n");
	}

	else {
		// Broadcast da variável correspondente ao caminho de um vértice até seu ponto mais distante
		MPI_Bcast(&sp, 1, MPI_INT, 0, MPI_COMM_WORLD);
		int graph[n][n];

		if(rank == 0){

			// Inicialização do grafo
			for(i = 0; i < n; i++)
				for(j = 0; j < n; j++)
					graph[i][j] = 0;

			// Leitura do peso de cada aresta e seus respectivos vértices
			for(i = 0; i < m; i++){
				scanf(" %d %d %d", &aux_i, &aux_j, &aux_graph);
				graph[aux_i-1][aux_j-1] = aux_graph;
				graph[aux_j-1][aux_i-1] = aux_graph;
		 	}

			// Envio da matriz de vértices para cada nó
			for(i = 1; i < size; i++)
				MPI_Send(graph, n * n, MPI_INT, i, 1, MPI_COMM_WORLD);
			
			// Caso o número de vértices for ímpar, o nó 0 se responsabiliza em computar o vértice restante
			if(n % 2)
				djikstra(graph, n-1, 0);
		}

		// Recebimento da matriz de vértices do nó 0
		else
			MPI_Recv(graph, n * n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);

		// Chamada da função, para cada nó, para o cálculo do menor caminho partindo de cada vértice
		for(i = (n/size) * rank; i < (n/size) + ((n/size) * rank); i++)
			djikstra(graph, i, rank);

		// Redução das variáveis 'sp' para a escolha do seu valor mínimo
		MPI_Reduce(&sp, &final_sp, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);

		if(rank == 0)
			printf("%d\n", final_sp);
	}

	MPI_Finalize();	

	return 0;
}