Last active
June 25, 2016 04:38
-
-
Save tcurvelo/bce08ba326dfb41d97dc to your computer and use it in GitHub Desktop.
Dijkstra's shortest path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define INFINITO 32766 | |
#define TRUE 1 | |
#define FALSE 0 | |
#define NN 150 | |
typedef unsigned char BOOL; | |
/* Dijkstra */ | |
void menorCaminho(int n, double grafo[n][n]) { | |
int pai[n]; | |
int i, u, v, q = n; | |
double custo0a[n]; | |
BOOL Q[n]; | |
/* inicializa os vetores que darao a solucao */ | |
for(i = 0; i < n; i++) { | |
custo0a[i] = INFINITO; | |
Q[i] = FALSE; | |
} | |
/* custo do nodo de origem para ele mesmo */ | |
custo0a[0] = 0; | |
u = 0; | |
while(q > 0) { | |
for(i=0;i<n;i++) { | |
if(custo0a[i] <= custo0a[u] && (!Q[i] || Q[u]) { | |
u = i; | |
} | |
} | |
q--; | |
Q[u] = TRUE; | |
for(v = 0; v < n; v++) { | |
if(grafo[u][v] != INFINITO) { | |
if(custo0a[v] > (custo0a[u] + grafo[u][v])) { | |
custo0a[v] = custo0a[u] + grafo[u][v]; | |
pai[v] = u; | |
} | |
} | |
} | |
} | |
for(v = 0; v < n; v++) printf("pai[%d]==%d", v, pai[v]); | |
/* return custo0a[1]; */ | |
} | |
int main(void) { | |
double grafo[5][5] = {{0,1,2,3,4}, | |
{1,0,5,6,4}, | |
{2,5,0,3,1}, | |
{3,6,3,0,7}, | |
{4,4,1,7,0} | |
}; | |
menorCaminho(5, grafo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment