-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion36.c
60 lines (49 loc) · 898 Bytes
/
question36.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
/*
All pair shortest path algorithm (Floyd Warshall)
here we keep updating taking different paths into consideration one after the other
*/
#include <stdio.h>
#include <stdlib.h>
void floydWarshall(int v, int graph[v][v]) {
int i,j,k;
int result[v][v];
for(i=0;i<v;i++) {
for(j=0;j<v;j++) {
result[i][j] = graph[i][j];
}
}
for(k=0;k<v;k++){
for(i=0;i<v;i++) {
for(j=0;j<v;j++){
if(result[i][k] + result[k][j] < result[i][j]){
result[i][j] = result[i][k] + result[k][j];
}
}
}
}
//print sol
for(i=0;i<v;i++) {
for(j=0;j<v;j++) {
printf("%d ", result[i][j]);
}
}
printf("\n");
}
int main(){
int cases;
int i;
scanf("%d", &cases);
for(i=0;i<cases;i++) {
int v;
scanf("%d",&v);
int graph[v][v];
int k,l;
for(k=0;k<v;k++){
for(l=0;l<v;l++){
scanf("%d", &graph[k][l]);
}
}
floydWarshall(v,graph);
}
return 0;
}