-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloyd_warshall.c
60 lines (52 loc) · 1015 Bytes
/
floyd_warshall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
int min1(int a,int b){
if(a<b)
return a;
return b;
}
void main(){
int adj[20][20],n,i,j,c=0,d=0,S[20],min=999,D[20][20][20],P[20][20][20],k;
char vertices[20];
scanf("%d",&n);
for(i=0;i<n;i++){ //get the adj matrix
for(j=0;j<n;j++){
scanf("%d",&adj[i][j]);
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
D[0][i][j]=adj[i][j];
}
}
scanf("%s",&vertices); //get the name of vertices
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(adj[i][j]<adj[i][k]+adj[k][j]){
adj[i][j]=adj[i][j];
P[k][i][j]=i;
}
else{
adj[i][j]=adj[i][k]+adj[k][j];
P[k][i][j]=k;
}
}
}
printf("\nD[%d]\n",k+1);
printf("\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d\t",adj[i][j]);
}
printf("\n");
}
printf("\nP[%d]\n",k+1);
printf("\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d\t",P[k][i][j]);
}
printf("\n");
}
}
}