-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFloydWarshallAlgo.java
46 lines (46 loc) · 1.01 KB
/
FloydWarshallAlgo.java
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
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
//code
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int[][] graph = new int[n][n];
for(int i = 0; i<n;i++)
for(int j = 0; j<n;j++)
graph[i][j] = sc.nextInt();
int[][] sol = new int[n][n];
for(int i = 0; i<n;i++)
for(int j = 0; j<n;j++)
sol[i][j] = graph[i][j];
for(int k = 0;k<n;k++)
{
for(int i = 0;i<n;i++)
{
for(int j = 0; j<n;j++)
{
if(sol[i][k]+sol[k][j]<sol[i][j])
sol[i][j] = sol[i][k]+sol[k][j];
}
}
}
for(int i = 0; i<n ;i++)
{
for(int j = 0; j<n; j++)
{
if(sol[i][j] == 10000000)
System.out.print("INF ");
else
System.out.print(sol[i][j]+" ");
}
System.out.println();
}
}
}
}