-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 41: Floyd Warshall
136 lines (89 loc) · 3.01 KB
/
Day 41: Floyd Warshall
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Floyd Warshall
Lamp
This problem is part of GFG SDE Sheet. Click here to view more.
The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j.
Do it in-place.
Example 1:
Input: matrix = {{0,25},{-1,0}}
Output: {{0,25},{-1,0}}
Explanation: The shortest distance between
every pair is already given(if it exists).
Example 2:
Input: matrix = {{0,1,43},{1,0,6},{-1,-1,0}}
Output: {{0,1,7},{1,0,6},{-1,-1,0}}
Explanation: We can reach 2 from 0 as 0->1->2
and the cost will be 1+6=7 which is less than
43.
Your Task:
You don't need to read, return or print anything. Your task is to complete the function shortest_distance() which takes the matrix as input parameter and modifies the distances for every pair in-place.
Expected Time Complexity: O(n3)
Expected Space Complexity: O(1)
Constraints:
1 <= n <= 100
-1 <= matrix[ i ][ j ] <= 1000
*/
//{ Driver Code Starts
//Initial template for JAVA
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while(T-->0)
{
int n = Integer.parseInt(br.readLine().trim());
int[][] matrix = new int[n][n];
for(int i = 0; i < n; i++){
String[] s = br.readLine().trim().split(" ");
for(int j = 0; j < n; j++)
matrix[i][j] =Integer.parseInt(s[j]);
}
Solution obj = new Solution();
obj.shortest_distance(matrix);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
}
// } Driver Code Ends
//User function template for JAVA
class Solution
{
public void shortest_distance(int[][] matrix)
{
// Code here
int n = matrix.length;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(matrix[i][j]==-1){
matrix[i][j]= Integer.MAX_VALUE;
}
}
}
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(matrix[i][k]< Integer.MAX_VALUE && matrix[k][j]< Integer.MAX_VALUE){
matrix[i][j]= Math.min(matrix[i][j], matrix[i][k]+ matrix[k][j]);
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(matrix[i][j]==Integer.MAX_VALUE){
matrix[i][j]= -1;
}
}
}
}
}