From b412956a7ce723da6e784166516c816a68a79e4b Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Mon, 5 May 2025 16:23:33 +0900 Subject: [PATCH] =?UTF-8?q?[20250505]=20BOJ=20/=20G4=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=9D=98=20=EC=A7=80=EB=A6=84=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\235\230 \354\247\200\353\246\204.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "lkhyun/202505/05 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" diff --git "a/lkhyun/202505/05 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" "b/lkhyun/202505/05 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" new file mode 100644 index 00000000..91942add --- /dev/null +++ "b/lkhyun/202505/05 BOJ G4 \355\212\270\353\246\254\354\235\230 \354\247\200\353\246\204.md" @@ -0,0 +1,64 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static class Node { + int to,cost; + Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + } + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N; + static List[] adjList; + + public static void main(String[] args) throws Exception { + N = Integer.parseInt(br.readLine()); + if(N == 1){ + bw.write("0"); + bw.close(); + return; + } + adjList = new ArrayList[N+1]; + for (int i = 1; i <= N; i++) { + adjList[i] = new ArrayList<>(); + } + for (int i = 1; i < N; i++) { + st = new StringTokenizer(br.readLine()); + int p = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + adjList[p].add(new Node(c, w)); + adjList[c].add(new Node(p, w)); + } + int[] dist = new int[N+1]; + int A = BFS(1,dist); + int B = BFS(A,dist); + bw.write(dist[B]+""); + bw.close(); + } + public static int BFS(int start,int[] dist){ + Arrays.fill(dist, Integer.MAX_VALUE); + Queue q = new ArrayDeque<>(); + q.add(start); + dist[start] = 0; + + int destination = start; + while(!q.isEmpty()){ + int cur = q.poll(); + for(Node node : adjList[cur]){ + if(dist[node.to] == Integer.MAX_VALUE){ + dist[node.to] = dist[cur] + node.cost; + q.add(node.to); + if(dist[node.to] > dist[destination]) destination = node.to; + } + } + } + return destination; + } +} +```