-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11657_1.java
More file actions
75 lines (57 loc) · 1.83 KB
/
Copy pathJ11657_1.java
File metadata and controls
75 lines (57 loc) · 1.83 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
64
65
66
67
68
69
70
71
72
73
74
75
package TIL;
import java.io.*;
import java.util.*;
public class J11657_1 {
static int n, m;
static ArrayList<int[]>[] list;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String input[] = buffer.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
list = new ArrayList[m];
long[] arr1 = new long[n];
long[] arr2 = new long[n];
for(int i = 0; i < n; i++){
arr1[i] = 9999999;
arr2[i] = 9999999;
}
arr1[0] = 0;
arr2[0] = 0;
for(int i = 0; i < m; i++){
list[i] = new ArrayList<>();
input = buffer.readLine().split(" ");
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[1]);
int c = Integer.parseInt(input[2]);
list[i].add(new int[]{a,b,c});
}
for(int i = 0; i < n-1; i++){
arr1 = bellman(arr1);
}
for(int i = 0; i < n; i++){
arr2= bellman(arr2);
}
for(int i = 1; i< n; i++){
if(arr1[i] != arr2[i]){
System.out.println("-1");
return;
}
}
for(int i = 1; i< n; i++){
if(arr1[i] == 9999999) System.out.println("-1");
else System.out.println(arr1[i]);
}
}
public static long[] bellman(long[] arr){
for(int i = 0 ; i< m; i++){
int[] node = list[i].get(0);
int start = node[0]-1;
if(arr[start] == 9999999) continue;
int end = node[1] - 1;
int w = node[2];
arr[end] = Math.min(arr[start]+w,arr[end]);
}
return arr;
}
}