From 13b12e9a1c65c2b7231f7f43cd052fb1666a7669 Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Thu, 9 Oct 2025 14:06:33 +0900 Subject: [PATCH] =?UTF-8?q?[20251008]=20BOJ=20/=20P4=20/=20K=EB=B2=88?= =?UTF-8?q?=EC=A7=B8=20=EC=B5=9C=EB=8B=A8=EA=B2=BD=EB=A1=9C=20=EC=B0=BE?= =?UTF-8?q?=EA=B8=B0=20/=20=ED=95=9C=EC=A2=85=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\353\241\234 \354\260\276\352\270\260.md" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 "Ukj0ng/202510/09 BOJ P4 K\353\262\210\354\247\270 \354\265\234\353\213\250\352\262\275\353\241\234 \354\260\276\352\270\260.md" diff --git "a/Ukj0ng/202510/09 BOJ P4 K\353\262\210\354\247\270 \354\265\234\353\213\250\352\262\275\353\241\234 \354\260\276\352\270\260.md" "b/Ukj0ng/202510/09 BOJ P4 K\353\262\210\354\247\270 \354\265\234\353\213\250\352\262\275\353\241\234 \354\260\276\352\270\260.md" new file mode 100644 index 00000000..3447e11b --- /dev/null +++ "b/Ukj0ng/202510/09 BOJ P4 K\353\262\210\354\247\270 \354\265\234\353\213\250\352\262\275\353\241\234 \354\260\276\352\270\260.md" @@ -0,0 +1,84 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static PriorityQueue[] dist; + private static List[] graph; + private static int n, m, k; + + public static void main(String[] args) throws IOException { + init(); + dijkstra(); + + for (int i = 1; i <= n; i++) { + if (dist[i].size() < k) { + bw.write("-1" + "\n"); + } else { + bw.write(dist[i].peek() + "\n"); + } + } + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + k = Integer.parseInt(st.nextToken()); + + dist = new PriorityQueue[n+1]; + graph = new List[n+1]; + + for (int i = 0; i <= n; i++) { + dist[i] = new PriorityQueue<>((o1, o2) -> Long.compare(o2, o1)); + graph[i] = new ArrayList<>(); + } + + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + long c = Long.parseLong(st.nextToken()); + + graph[a].add(new Node(b, c)); + } + } + + private static void dijkstra() { + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> Long.compare(o1.cost, o2.cost)); + dist[1].add(0L); + pq.add(new Node(1, 0L)); + + while (!pq.isEmpty()) { + Node current = pq.poll(); + + for (Node next : graph[current.dest]) { + long nCost = current.cost + next.cost; + if (dist[next.dest].size() < k) { + dist[next.dest].add(nCost); + pq.add(new Node(next.dest, nCost)); + } else if (dist[next.dest].peek() > nCost) { + dist[next.dest].poll(); + dist[next.dest].add(nCost); + pq.add(new Node(next.dest, nCost)); + } + } + } + } + + static class Node { + int dest; + long cost; + + Node (int dest, long cost) { + this.dest = dest; + this.cost = cost; + } + } +} +```