From 1535e0c45579362ed3aa0ed6e242b4437874d6c2 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Thu, 18 Sep 2025 23:20:10 +0900 Subject: [PATCH] =?UTF-8?q?[20250918]=20BOJ=20/=20G3=20/=20=EC=B5=9C?= =?UTF-8?q?=EC=86=8C=EB=B9=84=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0=202?= =?UTF-8?q?=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 --- ...\352\265\254\355\225\230\352\270\260 2.md" | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 "zinnnn37/202509/18 BOJ G3 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.md" diff --git "a/zinnnn37/202509/18 BOJ G3 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.md" "b/zinnnn37/202509/18 BOJ G3 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.md" new file mode 100644 index 00000000..5e7fe70c --- /dev/null +++ "b/zinnnn37/202509/18 BOJ G3 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.md" @@ -0,0 +1,124 @@ +```java +package etc; + +import java.io.*; +import java.util.*; + +public class BJ_1779_최소비용_구하기_2 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final StringBuilder sb = new StringBuilder(); + private static StringTokenizer st; + + private static int N, M; + private static int start, end; + + private static Dist[] dist; + private static List[] graph; + private static Queue q; + + private static class Node implements Comparable { + int to; + int weight; + + Node(int to, int weight) { + this.to = to; + this.weight = weight; + } + + @Override + public int compareTo(Node o) { + return Integer.compare(this.weight, o.weight); + } + } + + private static class Dist { + int dist; + List cities; + + Dist(int dist, int cur) { + this.dist = dist; + this.cities = new ArrayList<>(); + this.cities.add(cur); + } + + Dist(int dist, List cities) { + this.dist = dist; + this.cities = new ArrayList<>(cities); + } + + public void add(int v) { + this.cities.add(v); + } + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + M = Integer.parseInt(br.readLine()); + + graph = new List[N + 1]; + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 1; 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()); + + graph[u].add(new Node(v, w)); + } + + st = new StringTokenizer(br.readLine()); + start = Integer.parseInt(st.nextToken()); + end = Integer.parseInt(st.nextToken()); + + q = new PriorityQueue<>(); + dist = new Dist[N + 1]; + + for (int i = 1; i <= N; i++) { + dist[i] = new Dist(Integer.MAX_VALUE, i); + dist[i].cities.clear(); + } + } + + private static void sol() throws IOException { + q.offer(new Node(start, 0)); + dist[start] = new Dist(0, start); + + while (!q.isEmpty()) { + Node cur = q.poll(); + + if (dist[cur.to].dist < cur.weight) continue; + + for (Node n : graph[cur.to]) { + int nDist = dist[cur.to].dist + n.weight; + + if (dist[n.to] == null || nDist < dist[n.to].dist) { + dist[n.to] = new Dist(nDist, dist[cur.to].cities); + dist[n.to].add(n.to); + q.offer(new Node(n.to, nDist)); + } + } + } + + sb.append(dist[end].dist).append("\n"); + sb.append(dist[end].cities.size()).append("\n"); + for (int city : dist[end].cities) { + sb.append(city).append(" "); + } + bw.write(sb.toString()); + bw.flush(); + bw.close(); + br.close(); + } +} +``` \ No newline at end of file