From 1004c902ed5c13cef3d4bc6d2df747140c2bb8ed Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Sat, 6 Sep 2025 22:29:14 +0900 Subject: [PATCH] =?UTF-8?q?[20250905]=20BOJ=20/=20G3=20/=20=ED=8C=8C?= =?UTF-8?q?=ED=8B=B0=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 --- .../6 BOJ G3 \355\214\214\355\213\260.md" | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 "zinnnn37/202509/6 BOJ G3 \355\214\214\355\213\260.md" diff --git "a/zinnnn37/202509/6 BOJ G3 \355\214\214\355\213\260.md" "b/zinnnn37/202509/6 BOJ G3 \355\214\214\355\213\260.md" new file mode 100644 index 00000000..e96c3a95 --- /dev/null +++ "b/zinnnn37/202509/6 BOJ G3 \355\214\214\355\213\260.md" @@ -0,0 +1,113 @@ +```java +import java.io.*; +import java.util.*; + +public class BJ_1238_파티 { + + 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 N; + private static int M; + private static int X; + + private static int[] aDist; + private static int[] dDist; + private static List[] asc; + private static List[] desc; + + static class Node implements Comparable { + int end; + int weight; + + public Node(int end, int weight) { + this.end = end; + this.weight = weight; + } + + @Override + public int compareTo(Node o) { + return Integer.compare(this.weight, o.weight); + } + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + X = Integer.parseInt(st.nextToken()); + + asc = new List[N + 1]; + desc = new List[N + 1]; + for (int i = 1; i <= N; i++) { + asc[i] = new ArrayList<>(); + desc[i] = new ArrayList<>(); + } + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + + asc[u].add(new Node(v, w)); + desc[v].add(new Node(u, w)); + } + } + + private static void sol() throws IOException { + aDist = dijkstra(X, desc); + dDist = dijkstra(X, asc); + + int maxTime = 0; + for (int i = 1; i <= N; i++) { + if (i != X) { + maxTime = Math.max(maxTime, aDist[i] + dDist[i]); + } + } + + bw.write(maxTime + ""); + bw.flush(); + bw.close(); + br.close(); + } + + private static int[] dijkstra(int start, List[] graph) { + PriorityQueue pq = new PriorityQueue<>(); + + int[] dist = new int[N + 1]; + Arrays.fill(dist, Integer.MAX_VALUE); + + dist[start] = 0; + pq.offer(new Node(start, 0)); + + while (!pq.isEmpty()) { + Node current = pq.poll(); + int curEnd = current.end; + int curWeight = current.weight; + + if (curWeight > dist[curEnd]) continue; + + for (Node next : graph[curEnd]) { + int nextEnd = next.end; + int nextWeight = next.weight; + + int newDist = dist[curEnd] + nextWeight; + + if (newDist < dist[nextEnd]) { + dist[nextEnd] = newDist; + pq.offer(new Node(nextEnd, newDist)); + } + } + } + return dist; + } +} +``` \ No newline at end of file