Skip to content

Commit 5807ae2

Browse files
authored
[20250821] BOJ / G4 / 운동 / 김수연
1 parent 5b35e94 commit 5807ae2

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj1956 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
static final int INF = 987654321;
11+
12+
public static void main(String[] args) throws Exception {
13+
nextLine();
14+
int V = nextInt();
15+
int E = nextInt();
16+
int[][] dist = new int[V+1][V+1];
17+
int answer = INF;
18+
for (int i = 0; i <= V; i++) {
19+
Arrays.fill(dist[i], INF);
20+
dist[i][i] = 0;
21+
}
22+
for (int i = 0; i < E; i++) {
23+
nextLine();
24+
int a = nextInt();
25+
int b = nextInt();
26+
int c = nextInt();
27+
dist[a][b] = c;
28+
}
29+
for (int k = 1; k <= V; k++) {
30+
for (int i = 1; i <= V; i++) {
31+
for (int j = 1; j <= V; j++) {
32+
if (dist[i][j] > dist[i][k] + dist[k][j]) {
33+
dist[i][j] = dist[i][k] + dist[k][j];
34+
}
35+
}
36+
}
37+
}
38+
for (int i = 1; i <= V; i++) {
39+
for (int j = 1; j <= V; j++) {
40+
if (dist[i][j] == INF || dist[j][i] == INF) continue;
41+
if (i == j) continue;
42+
answer = Math.min(answer, dist[i][j] + dist[j][i]);
43+
}
44+
}
45+
if (answer == INF) System.out.println(-1);
46+
else System.out.println(answer);
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)