-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshall.java
More file actions
63 lines (51 loc) · 1.63 KB
/
FloydWarshall.java
File metadata and controls
63 lines (51 loc) · 1.63 KB
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
package com.mycompany.algorithm_final_project;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class FloydWarshall {
final int INF = Integer.MAX_VALUE;
final int N = 510;
int[][] dist = new int[N][N];
public void main_func(){
Scanner s = new Scanner(System.in);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(i == j) dist[i][j] = 0;
else dist[i][j] = INF;
}
}
System.out.print(" Enter the number of vertices and edges: ");
int n = s.nextInt();
int m = s.nextInt();
System.out.println(" Enter the edges and weights: ");
for (int i = 0; i < m; i++) {
int x = s.nextInt();
int y = s.nextInt();
int wt = s.nextInt();
dist[x][y] = wt;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if(dist[i][k] != INF && dist[k][j] != INF){
dist[i][j] = Math.min(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
}
System.out.println("");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][j] == INF) {
System.out.print("I ");
}
else{
System.out.print(dist[i][j] + " ");
}
}
System.out.println("");
}
}
}