forked from Annex5061/java-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFord.java
More file actions
Latest commit
45 lines (45 loc) · 1.12 KB
/
Copy pathBellmanFord.java
File metadata and controls
45 lines (45 loc) · 1.12 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
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannerscn = newScanner(System.in);
intn = scn.nextInt();
intm = scn.nextInt();
int[][] matrix = newint[m][3];
for (inti = 0; i
< m; i++) {
intu = scn.nextInt();
intv = scn.nextInt();
intw = scn.nextInt();
matrix[i][0] = u - 1;
matrix[i][1] = v - 1;
matrix[i][2] = w;
}
int[] path = newint[n];
Arrays.fill(path, Integer.MAX_VALUE);
path[0] = 0;
scn.close();
for (inti = 0; i < n - 1; i++) {
for (intj = 0; j
< m; j++) {
intu = matrix[j][0];
intv = matrix[j][1];
intw = matrix[j][2];
if (path[u] == Integer.MAX_VALUE) {
continue;
}
if (path[u] + w < path[v]) {
path[v] = path[u] + w;
}
}
}
for (inti = 1; i < path.length; i++) {
if (path[i] == Integer.MAX_VALUE)
{
System.out.print( "1000000000" + " " );
}
else {
System.out.print(path[i] + " " );
}
}
}
}