๐Ÿ“– [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] Q.1753 ์ตœ๋‹จ ๊ฒฝ๋กœ

๐Ÿ“– ๋ฌธ์ œ

https://www.acmicpc.net/problem/1753

๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ฃผ์–ด์ง„ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋‹จ, ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

๐Ÿ” ์ ‘๊ทผ๋ฒ•

BOJ Q.1753 ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

ํ•˜๋‚˜์˜ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค

์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ๋Œ€ํ‘œ์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ˜„์žฌ ๊ฒฝ๋กœ ์ค‘์—์„œ ๊ฐ€์žฅ ์งง์€ ๋น„์šฉ์˜ ์ •์ ์œผ๋กœ ํ•˜๋‚˜ ์”ฉ ๋ฐฉ๋ฌธํ•ด๊ฐ€๋ฉฐ

์ƒˆ๋กœ์šด ์ •์ ์„ ํ†ตํ•ด ๊ฒฝ๋กœ๊ฐ€ ๋” ์งง์•„์ง„๋‹ค๋ฉด ์ตœ์†Œ ๊ฒฝ๋กœ๋กœ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.

            //๋ชฉ์ ์ง€ ์งํ–‰ , ํ˜„์žฌ๊นŒ์ง€ ๊ฑฐ๋ฆฌ + ํ˜„์žฌ์—์„œ ๋ชฉ์ ์ง€ ๊ฑฐ๋ฆฌ
            if(distance[destination.index] > distance[current.index] + destination.distance) {
                // ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋น„์šฉ ๊ฐฑ์‹ 
                distance[destination.index] = distance[current.index] + destination.distance;
                priorityQueue.add(new Node(destination.index, distance[destination.index]));
            }

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๊ฒฝ๋กœ ์ค‘ ๊ฐ€์žฅ ์งง์€ ๋น„์šฉ์˜ ์ •์ ์„ ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” ์ธ์ ‘ํ–‰๋ ฌ๋กœ ํ’€์—ˆ๋Š”๋ฐ ์ •์ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€ 2๋งŒ๊ฐœ ๊นŒ์ง€์—ฌ์„œ

๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์—ˆ๊ณ  ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๐Ÿ’ป ์ฝ”๋“œ

package problem.shortestpath.Q1753;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Main_1753 {
    static final int INF = Integer.MAX_VALUE;
    static int V, E, start;
    static List<List<Node>> adList;
    static int[] distance;
    static boolean[] visited;
    static PriorityQueue<Node> priorityQueue;


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new  InputStreamReader(System.in));

        String input[] = br.readLine().split(" ");
        V = Integer.parseInt(input[0]);
        E = Integer.parseInt(input[1]);
        start = Integer.parseInt(br.readLine());

        adList = new ArrayList<>();
        // ์ธ๋ฑ์Šค๋ฅผ 1๋ถ€ํ„ฐ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•œ ๊ฐœ๋ฅผ ๋„ฃ์–ด๋‘ 
        adList.add(new<Node> ArrayList());
        for(int i = 1; i<= V; i++) {
            adList.add(new<Node> ArrayList());
        }

        distance = new int[V +1];
        Arrays.fill(distance, INF);

        for(int i=0; i<E; i++) {
            String[] temp = br.readLine().split(" ");
            int u = Integer.parseInt(temp[0]);
            int v = Integer.parseInt(temp[1]);
            int cost = Integer.parseInt(temp[2]);
            adList.get(u).add(new Node(v, cost));
        }

        solve(start);

        for (int i = 1; i <= V; i++) {
            if(distance[i] == INF){
                System.out.println("INF");
            }
            else {
                System.out.println(distance[i]);
            }
        }
    }

    private static void solve(int start) {
        priorityQueue = new PriorityQueue<>();
        visited = new boolean[V +1];

        distance[start] = 0;
        priorityQueue.add(new Node(start, 0));

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

            if(visited[current.index]){
                continue;
            }
            visited[current.index] = true;
            // ์—ฐ๊ฒฐ๋œ ์ •์ ๋“ค์„ ํ™•์ธ
            for(Node destination : adList.get(current.index)) {
                //๋ชฉ์ ์ง€ ์งํ–‰ , ํ˜„์žฌ๊นŒ์ง€ ๊ฑฐ๋ฆฌ + ํ˜„์žฌ์—์„œ ๋ชฉ์ ์ง€ ๊ฑฐ๋ฆฌ
                if(distance[destination.index] > distance[current.index] + destination.distance) {
                    // ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋น„์šฉ ๊ฐฑ์‹ 
                    distance[destination.index] = distance[current.index] + destination.distance;
                    priorityQueue.add(new Node(destination.index, distance[destination.index]));
                }
            }
        }
    }

}

class Node implements Comparable<Node>{
    int index;
    int distance;

    public Node(int index, int distance) {
        this.index = index;
        this.distance = distance;
    }

    @Override
    public int compareTo(Node target) {
        // ์ž‘์€ ๊ฑฐ๋ฆฌ ๋น„์šฉ์ด ๋จผ์ € ์˜ค๋„๋ก
        return this.distance - target.distance;
    }
}

Written on September 25, 2020