Skip to content

Commit fc66337

Browse files
authored
Merge pull request #1699 from AlgorithmWithGod/Seol-JY
[20251217] BOJ / G4 / 운동 / 설진영
2 parents 3e80698 + 3d5c2b6 commit fc66337

File tree

1 file changed

+47
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)