Created
May 11, 2014 06:24
-
-
Save tcurvelo/6ac87daa373fd33d51fa to your computer and use it in GitHub Desktop.
Bellman-Ford's shortest path
This file contains hidden or 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; | |
double bellman_ford(int n, double grafo[n][n]){ | |
int pai[n], i, u, v; | |
double custo_de_zero_a[n]; | |
for(i=0; i<n; i++){ | |
pai[i] = -1; | |
custo_de_zero_a[i] = INFINITO; | |
} | |
custo_de_zero_a[0]=0; | |
for(i=0; i<n; i++){ | |
for(u=0; u<n; u++){ | |
for(v=0; v<n; v++){ | |
if(grafo[u][v] != INFINITO){ | |
if(custo_de_zero_a[v] > (custo_de_zero_a[u] + grafo[u][v])){ | |
custo_de_zero_a[v] = custo_de_zero_a[u] + grafo[u][v]; | |
pai[v] = u; | |
} | |
} | |
} | |
} | |
} | |
return custo_de_zero_a[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} | |
}; | |
bellman_ford(5, grafo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment