๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ProgramSolve/Baekjoon

[BOJ] 11779๋ฒˆ - ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ2 (Java)

by SooooooooS 2023. 12. 8.
728x90

๐Ÿ› ๏ธ ๋ฌธ์ œ ๐Ÿ› ๏ธ


๐Ÿ—’๏ธ ์„ค๋ช… ๐Ÿ—’๏ธ

์šฐ์„  ์˜ค๋Š˜ ์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฝ์งˆํ•œ ์‹œ๊ฐ„์€ ์˜ˆ์ œ๋ฅผ ๋งž์ถ”๋ ค๊ณ  ๋…ธ๋ ฅํ•œ ์‹œ๊ฐ„์ด๋‹ค.

์ด์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณธ ๊ฒฝํ—˜์ด ์žˆ์–ด ์›๋ฆฌ๋ฅผ ์•Œ๊ณ  ์žˆ์—ˆ๊ณ  ์˜ˆ์ œ๋ฅผ ๋ฏฟ๊ณ  ๋ฐ”๋กœ ํ’€์ด์— ๋“ค์–ด๊ฐ”๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๊ณ„์†ํ•ด์„œ ๊ฒฝ๋กœ๊ฐ€ ์˜ˆ์ œ ์ •๋‹ต๊ณผ ๋‹ค๋ฅด๊ฒŒ [1, 4, 5]๋กœ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค. 

์ดˆ๋ฐ˜์—๋Š” ๋ฌธ์ œ๊ฐ€ ํ‹€๋ฆด ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ๋‚ด ์ฝ”๋“œ๋งŒ ๊ณ„์†ํ•ด์„œ ํ™•์ธํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์•„๋ฌด๋ฆฌ ์˜ค๋ฅ˜๋ฅผ ์ฐพ์•„๋ณด๋ ค๊ณ  ํ•ด๋„ ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์•˜๊ณ  ์˜ˆ์ œ๋ฅผ ์ง์ ‘ ์†์œผ๋กœ ํ’€์–ด๋ณด๋‹ˆ ์‹ค์ œ ๊ฒฝ๋กœ๊ฐ€ [1, 4, 5]๊ฐ€ ๋งž์•˜๋‹ค.

 

์ด๋ฒˆ์— ๋Š๋‚€ ๊ฒƒ์€ ์•„๋ฌด๋ฆฌ ์›๋ฆฌ๋ฅผ ์•Œ๊ณ  ์žˆ๋”๋ผ๋„ ์ง์ ‘ ๋‹ต์„ ๊ตฌํ•ด๋ณด๋ฉฐ ๋ฌธ์ œ ํ’€์ด์— ์ ‘๊ทผํ•ด์•ผ๊ฒ ๋‹ค!!!!

์‚ฝ์งˆํ•œ ์‹œ๊ฐ„์€ ์•„๊น๊ณ  ํ—ˆ๋ฌดํ–ˆ์ง€๋งŒ.. ๋‹ค์Œ๋ถ€ํ„ฐ ์‹ค์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š์œผ๋ฉด ๋œ๋‹ค..!

 

โ€ป  Comparable

public static class Node implements Comparable<Node>{
    int destination, cost;
    public Node(int destination, int cost) {
        this.destination = destination;
        this.cost = cost;
    }

    @Override
    public int compareTo(Node o) {
        return cost - o.cost;
    }
}

 

์šฐ์„ ์ˆœ์œ„ ํ์—์„œ cost๋ฅผ ๋น„๊ตํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋ฏ€๋กœ Comparable ์‚ฌ์šฉ!

 

[๊ฐ์ฒด ๋น„๊ต]

Comparable Comparator
์ž๊ธฐ์ž์‹ ๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ์ฒด ๋น„๊ต ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ์ฒด ๋น„๊ต
compareTo(T o) ํ•จ์ˆ˜ ๋ฌด์กฐ๊ฑด ๊ตฌํ˜„ compare(T o1, T o2) ํ•จ์ˆ˜ ๋ฌด์กฐ๊ฑด ๊ตฌํ˜„

 

์ฐธ๊ณ  : https://st-lab.tistory.com/243


๐Ÿ€ ํ’€์ด ๐Ÿ€

package Baekjoon;

import java.io.*;
import java.util.*;

public class Graph11779 {
    public static class Node implements Comparable<Node>{
        int destination, cost;
        public Node(int destination, int cost) {
            this.destination = destination;
            this.cost = cost;
        }

        @Override
        public int compareTo(Node o) {
            return cost - o.cost;
        }
    }

    public static int[] preNode; //์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ์˜ฌ ๊ฒฝ์šฐ ์ด์ „ ๋…ธ๋“œ ์ €์žฅํ•  ๋ฐฐ์—ด
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine());
        int m = Integer.parseInt(br.readLine());
        ArrayList<Node>[] graph = new ArrayList[n+1];
        for(int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        StringTokenizer st;
        for(int i = 0; i < m; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int w = Integer.parseInt(st.nextToken());
            graph[a].add(new Node(b, w));
        }

        st = new StringTokenizer(br.readLine());
        int start = Integer.parseInt(st.nextToken());
        int end = Integer.parseInt(st.nextToken());

        preNode = new int[n+1];
        bw.write(dijkstra(graph, start, end) + "\n");
        bw.write(trace(start, end));
        bw.flush();
        bw.close();
    }

    /**
     * ๋‹ค์ต์ŠคํŠธ๋ผ
     * @param graph ๋ฐฉํ–ฅ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„
     * @param start ์‹œ์ž‘ ์ •์ 
     * @param end ๋„์ฐฉ ์ •์ 
     * @return ์‹œ์ž‘์—์„œ ๋„์ฐฉ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ
     */
    public static int dijkstra(ArrayList<Node>[] graph, int start, int end){
        int[] distance = new int[graph.length];
        Arrays.fill(distance, Integer.MAX_VALUE);

        PriorityQueue<Node> heap = new PriorityQueue<>();
        distance[start] = 0;
        heap.offer(new Node(start, 0));

        while (!heap.isEmpty()) {
            Node current = heap.poll();

            if(distance[current.destination] < current.cost) {
                continue;
            }

            for(Node next : graph[current.destination]) {
                if(distance[next.destination] > distance[current.destination] + next.cost) {
                    distance[next.destination] = distance[current.destination] + next.cost;
                    heap.offer(new Node(next.destination, distance[next.destination]));
                    preNode[next.destination] = current.destination; //๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ์ด์ „ ๋…ธ๋“œ ์ €์žฅ
                }
            }
        }
        return distance[end];
    }

    /**
     * ๊ฒฝ๋กœ ํƒ์ƒ‰
     * @param start ์‹œ์ž‘ ์ •์ 
     * @param end ๋„์ฐฉ ์ •์ 
     * @return ๋…ธ๋“œ ๊ฐœ์ˆ˜ ๋ฐ ๊ฒฝ๋กœ ๋ฌธ์ž์—ด
     */
    public static String trace(int start, int end) {
        Stack<Integer> stack = new Stack<>();
        stack.push(end);
        int current = end;
        while(preNode[current] != start) {
            current = preNode[current];
            stack.push(current);
        }
        StringBuilder answer = new StringBuilder();
        answer.append(stack.size()+1 + "\n");
        answer.append(start + " ");
        while(!stack.isEmpty()) {
            answer.append(stack.pop() + " ");
        }
        return answer.toString();
    }
}
728x90