Hi guys, friend of mine gived me code that works correctly but i had to do for my homevork an application that shows full fastest road and distance. Code he gived to me shows only distance and I can't figure out how to edit it so it will show path also.
Thnx for your suggestions
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define nieskonczonosc 100000 //infinity
#define MIN(a,b) (a<b)?a:b
void main(void)
{
FILE *plik;
int n; //number of verticles
int A[100][100];
int D[100];
char s[5];
int i,j,k;
int N;
if ((plik=fopen("graf.txt","r"))==NULL)
printf("missing file graf.txt!\n"); else
{
fscanf(plik,"%i",&n);
printf("number of verticles: %i\n",n);
for (j=0;j<n;j++)
for (i=0;i<n;i++)
{
fscanf(plik,"%s",s);
if (strcmp(s,"*")!=0)
A[j][i]=atoi(s); else A[j][i]=nieskonczonosc;
}
fclose(plik);
for (i=0;i<n;i++) D[i]=A[0][i];
//start
for (k=1;k<=n-2;k++)
{
for (i=1;i<n;i++)
for (j=0;j<n;j++)
{
if (D[j]+A[j][i]>nieskonczonosc)
N=nieskonczonosc;
else{
N=D[j]+A[j][i];
D[i]=MIN(D[i],N);
}
}
}
for (i=0;i<n;i++)
if (D[i]<nieskonczonosc)
printf("distance to (%i) = %i\n",i+1,D[i]);
else
printf("distance to (%i) = %s\n",i+1,"*");
}
}
.txt file named graf.txt
9
0 5 * 23 * 7 * * *
5 0 8 * * * 23 * *
* 8 0 4 7 9 * * *
23 * 4 0 23 * * * *
* * 7 23 0 2 * * 4
7 * 9 * 2 0 2 * *
* 23 * * * 2 0 1 *
* * * * * * 1 0 2
* * * * 4 * * 2 0
and another sample of graf.txt
6
0 * 3 * * *
* 0 2 1 3 *
3 2 0 * 1 *
* 1 * 0 * *
* 3 1 * 0 6
* * * * 6 0