-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1956.java
More file actions
56 lines (45 loc) · 1.64 KB
/
BOJ_1956.java
File metadata and controls
56 lines (45 loc) · 1.64 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
import java.io.*;
import java.util.*;
public class BOJ_1956 {
static final int INF = 987654321;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int V = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
int[][] dist = new int[V][V];
for(int i = 0; i < V; i++) {
for(int j = 0; j < V; j++) {
if(i == j) dist[i][j] = 0;
dist[i][j] = INF;
}
}
for(int i = 0; i<E; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
dist[a-1][b-1] = c;
}
for(int k = 0; k < V; k++) {
for(int i = 0; i < V; i++) {
if(i == k) continue;
for(int j = 0; j < V; j++) {
if(i == j || k == j) continue;
dist[i][j] = Math.min(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
int answer = INF;
for(int i = 0; i < V; i++) {
for(int j = 0; j < V; j++) {
if(i == j) continue;
if(dist[i][j] != INF && dist[j][i] != INF) {
int tmp = dist[i][j] + dist[j][i];
answer = Math.min(answer, tmp);
}
}
}
System.out.println(answer == INF ? -1 : answer);
}
}