-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11657.java
More file actions
83 lines (58 loc) · 1.85 KB
/
Copy pathJ11657.java
File metadata and controls
83 lines (58 loc) · 1.85 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
76
77
78
79
80
81
82
83
package TIL;
import java.util.*;
import java.io.*;
public class J11657 {
static ArrayList<int[]>[] list;
static int N;
static int M;
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[] array = new long[N];
long[] array2 = new long[N];
for(int i = 0; i<N; i++){
array[i] = 9999999;
array2[i] = 9999999;
}
array[0] = 0;
array2[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++){
array=bell(array);
}
for(int i = 0; i<N;i++) {
array2 = bell(array2);
}
for(int i =1; i < N;i++) {
if (array[i] != array2[i]) {
System.out.println(-1);
return;
}
}
for(int i =1; i < N; i++){
if(array[i] == 9999999) System.out.println(-1);
else System.out.println(array[i]);
}
}
public static long[] bell(long[] array){
for(int i = 0; i<M; i++){
int[] node = list[i].get(0);
int start = node[0]-1;
if(array[start] == 9999999) continue;
int end = node[1]-1;
int weight = node[2];
array[end] = Math.min(array[end], array[start]+weight);
}
return array;
}
}