From 5807ae268963e9d75313636d52e794749820bc6a Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 21 Aug 2025 10:19:41 +0900 Subject: [PATCH] =?UTF-8?q?[20250821]=20BOJ=20/=20G4=20/=20=EC=9A=B4?= =?UTF-8?q?=EB=8F=99=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../21 BOJ G4 \354\232\264\353\217\231.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "suyeun84/202508/21 BOJ G4 \354\232\264\353\217\231.md" diff --git "a/suyeun84/202508/21 BOJ G4 \354\232\264\353\217\231.md" "b/suyeun84/202508/21 BOJ G4 \354\232\264\353\217\231.md" new file mode 100644 index 00000000..a77e2c3e --- /dev/null +++ "b/suyeun84/202508/21 BOJ G4 \354\232\264\353\217\231.md" @@ -0,0 +1,49 @@ +```java +import java.util.*; +import java.io.*; + +public class boj1956 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static final int INF = 987654321; + + public static void main(String[] args) throws Exception { + nextLine(); + int V = nextInt(); + int E = nextInt(); + int[][] dist = new int[V+1][V+1]; + int answer = INF; + for (int i = 0; i <= V; i++) { + Arrays.fill(dist[i], INF); + dist[i][i] = 0; + } + for (int i = 0; i < E; i++) { + nextLine(); + int a = nextInt(); + int b = nextInt(); + int c = nextInt(); + dist[a][b] = c; + } + for (int k = 1; k <= V; k++) { + for (int i = 1; i <= V; i++) { + for (int j = 1; j <= V; j++) { + if (dist[i][j] > dist[i][k] + dist[k][j]) { + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + for (int i = 1; i <= V; i++) { + for (int j = 1; j <= V; j++) { + if (dist[i][j] == INF || dist[j][i] == INF) continue; + if (i == j) continue; + answer = Math.min(answer, dist[i][j] + dist[j][i]); + } + } + if (answer == INF) System.out.println(-1); + else System.out.println(answer); + } +} +```