Skip to content

Commit 84aa8fd

Browse files
authored
[20260202] BOJ / G4 / 운동 / 이강현
1 parent c641ae7 commit 84aa8fd

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

lkhyun/202602/02 BOJ G4 운동.md

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

0 commit comments

Comments
 (0)