From 89a7de8cc92d5bfca058dfee11dbecc39d13fc6e Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Wed, 17 Dec 2025 23:21:47 +0900 Subject: [PATCH] =?UTF-8?q?[20251217]=20BOJ=20/=20G4=20/=20=EC=9A=B4?= =?UTF-8?q?=EB=8F=99=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../17 BOJ G4 \354\232\264\353\217\231.md" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "zinnnn37/202512/17 BOJ G4 \354\232\264\353\217\231.md" diff --git "a/zinnnn37/202512/17 BOJ G4 \354\232\264\353\217\231.md" "b/zinnnn37/202512/17 BOJ G4 \354\232\264\353\217\231.md" new file mode 100644 index 00000000..597a9ac4 --- /dev/null +++ "b/zinnnn37/202512/17 BOJ G4 \354\232\264\353\217\231.md" @@ -0,0 +1,66 @@ +```java +import java.io.*; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class BJ_1956_운동 { + + private static final int INF = 987654321; + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int V, E, ans; + private static int[][] matrix; + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + V = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + ans = INF; + + matrix = new int[V + 1][V + 1]; + for (int i = 1; i <= V; i++) { + Arrays.fill(matrix[i], 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 w = Integer.parseInt(st.nextToken()); + + matrix[a][b] = w; + } + + } + + private static void sol() throws IOException { + for (int k = 1; k <= V; k++) { + for (int i = 1; i <= V; i++) { + for (int j = 1; j <= V; j++) { + matrix[i][j] = Math.min(matrix[i][j], matrix[i][k] + matrix[k][j]); + } + } + } + + for (int i = 1; i <= V; i++) { + ans = Math.min(ans, matrix[i][i]); + } + + bw.write(ans != INF ? ans + "" : "-1"); + + bw.flush(); + bw.close(); + br.close(); + } + +} +``` \ No newline at end of file