-
Notifications
You must be signed in to change notification settings - Fork 6
/
JIHANEOL.java
85 lines (64 loc) · 1.75 KB
/
JIHANEOL.java
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
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class JIHANEOL {
static BufferedReader br;
static StringTokenizer st;
static class Edge{
int s,e,cost;
public Edge(int s, int e, int cost) {
super();
this.s = s;
this.e = e;
this.cost = cost;
}
}
static final int INF= Integer.MAX_VALUE;
static ArrayList<Edge> list;
static long distance[];
public static void main(String[] args) throws Exception{
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
int V = Integer.valueOf(st.nextToken());
int E = Integer.valueOf(st.nextToken());
list = new ArrayList();
for(int i=0; i<E; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.valueOf(st.nextToken());
int e = Integer.valueOf(st.nextToken());
int c = Integer.valueOf(st.nextToken());
list.add(new Edge(s, e, c));
}
distance = new long[V+1];
Arrays.fill(distance, INF);
distance[1]=0;
// 벨만 씨 사용 , 엣지를 중심으로 동작한다.
for(int i=1; i<V; i++) { // V-1번 돈다. 자신 제외
for(int j=0; j<E; j++) { // 간선 만큼.
Edge now = list.get(j);
if(distance[now.s]!= INF && distance[now.e]>distance[now.s]+now.cost) {
distance[now.e]=distance[now.s]+now.cost;
}
}
}
boolean flag = false;
for(int j=0; j<E; j++) { // 간선 만큼. 다시 한번 돌려준다.
Edge now = list.get(j);
if(distance[now.s]!= INF && distance[now.e]>distance[now.s]+now.cost) {
flag=true;
break;
}
}
if(flag) {
System.out.println("-1");
}else {
for(int i=2; i<=V; i++) {
if(distance[i]==INF) {
System.out.println("-1");
}else {
System.out.println(distance[i]);
}
}
}
}
}