Created
May 20, 2013 08:16
-
-
Save takahisa/5611006 to your computer and use it in GitHub Desktop.
D!!S!!A!!
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> | |
#include <stdlib.h> | |
#define MAX(x, y) ((x == M) ? x : (y == M) ? y : (x > y) ? x : y) | |
#define MIN(x, y) ((x == M) ? y : (y == M) ? x : (x < y) ? x : y) | |
typedef int Weight; | |
#define M (-1) | |
#define N (6) | |
int W[N][N] = { | |
{ 0, M, M, 8, 15, M }, | |
{ M, M, 0, M, M, 6 }, | |
{ M, M, M, 0, 5, M }, | |
{ M, M, 12, M, 0, 7 }, | |
{ M, M, 3, M, M, 0 } | |
}; | |
typedef enum { TRUE, FALSE } Bool; | |
Bool S[N]; | |
int count; | |
Weight D[N]; | |
void add(int p) { | |
S[p] = TRUE; | |
} | |
int select_min(void) { | |
int i, n; | |
n = M; | |
for(i = 1; i < N; i++) | |
n = MIN(n, D[i]); | |
return n; | |
} | |
Bool remain(void) { | |
int i; | |
for(i = 0; i < N; i++) { | |
if(S[i] == FALSE) | |
return TRUE; | |
} | |
return FALSE; | |
} | |
Bool member(int u, int x) { | |
return FALSE; | |
} | |
void dijkstra(int p) { | |
int i, u, x, k; | |
add(p); | |
for(i = 0; i < N; i++) | |
D[i] = W[p][i]; | |
while(remain()) { | |
u = select_min(); | |
add(u); | |
for(x = 0; x < N; x++) { | |
if(member(u, x)) { | |
k = D[u] + W[u][x]; | |
if(k < D[x]) | |
D[x] = k; | |
} | |
} | |
} | |
} | |
int main(int argc, char** argv) { | |
if (argc != 2) { | |
fprintf(stderr, "Usage: program nodeid\n"); | |
exit(EXIT_FAILURE); | |
} | |
dijkstra(atoi(argv[1])); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment