I have made floyd warshall algorithm.
I need some help for finding shortest path from source to destination.
We have to give source and destination.
Eg. Program gives us output like source=A and destination=D then shortest path is A-B-D with distance 10.
I also give the code for that in which we are calculating shortest path from all node to other node.
Waiting for your reply.
Thank you.
---------------------------
Floyd warshall.cpp

#include <stdio.h>
#include<conio.h>

int n;
int dist[16][16]; 

void printDist() {
	int i, j;
	printf("    ");
	for (i = 0; i < n; ++i)
		printf("%4c", 'A' + i);
	printf("\n");
	for (i = 0; i < n; ++i) {
		printf("%4c", 'A' + i);
		for (j = 0; j < n; ++j)
			printf("%4d", dist[i][j]);
		printf("\n");
	}
	printf("\n");
}

void floyd_warshall() {
	int i, j, k;
	for (k = 0; k < n; ++k) {
		printDist();
		for (i = 0; i < n; ++i)
			for (j = 0; j < n; ++j)
				
				if ((dist[i][k] * dist[k][j] != 0) && (i != j))
					
					if ((dist[i][k] + dist[k][j] < dist[i][j]) || (dist[i][j] == 0))
						dist[i][j] = dist[i][k] + dist[k][j];
	}
	printDist();
}

int main(int argc, char *argv[]) {
	clrscr();
	FILE *fin = fopen("a1_data.txt", "r");
	fscanf(fin, "%d", &n);
	int i, j;
	for (i = 0; i < n; ++i)
		for (j = 0; j < n; ++j)
			fscanf(fin, "%d", &dist[i][j]);
	fclose(fin);

	floyd_warshall();
	getch();
	return 0;
}

--------------------------------
a1_data.txt
---------------------------------
5
4 5 3 2 0
1 0 4 5 2
8 9 0 30 0
0 6 7 0 5
4 3 2 0 9

Sorry, but we don't solve your class problems for you. If you need some help and/or comments with specific parts of your code, that is another thing, and that we might help you with.

This article has been dead for over six months. Start a new discussion instead.