Created
February 28, 2013 08:46
-
-
Save takahisa/5055267 to your computer and use it in GitHub Desktop.
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> | |
int n; | |
int route[100][100]; | |
int dtable[100]; | |
int distance (int i, int j); | |
int main(void) | |
{ | |
int i, j; | |
scanf("%d", &n); | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < n; j++) { | |
scanf("%d", &route[i][j]); | |
} | |
} | |
printf("minimum distance = %d\n", distance(0, n - 1)); | |
i = 0; | |
do | |
printf("%d\n", dtable[i]); | |
while(dtable[i++] != (n-1)); | |
return 0; | |
} | |
int distance_impl (int i, int j, int p) | |
{ | |
int d, k, min; | |
min = 1000; | |
if (i == j) | |
return 0; | |
for (k = 0; k < n; k++) { | |
if (route[i][k] == 0) | |
continue; | |
else | |
d = route[i][k] + distance_impl(k, j, p + 1); | |
if (d < min) { | |
min = d; | |
dtable[p] = k; | |
} | |
} | |
return min; | |
} | |
int distance(int i, int j) { | |
dtable[0] = 0; | |
distance_impl(i, j, 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment