๐Ÿ“– [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] Q.1916 ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

๐Ÿ“– ๋ฌธ์ œ

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

N๊ฐœ์˜ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ ๋„์‹œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋‹ค๋ฅธ ๋„์‹œ์— ๋„์ฐฉํ•˜๋Š” M๊ฐœ์˜ ๋ฒ„์Šค๊ฐ€ ์žˆ๋‹ค.

์šฐ๋ฆฌ๋Š” A๋ฒˆ์งธ ๋„์‹œ์—์„œ B๋ฒˆ์งธ ๋„์‹œ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋“œ๋Š” ๋ฒ„์Šค ๋น„์šฉ์„ ์ตœ์†Œํ™” ์‹œํ‚ค๋ ค๊ณ  ํ•œ๋‹ค.

A๋ฒˆ์งธ ๋„์‹œ์—์„œ B๋ฒˆ์งธ ๋„์‹œ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ๋น„์šฉ์„ ์ถœ๋ ฅํ•˜์—ฌ๋ผ. ๋„์‹œ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€์ด๋‹ค.

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

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

ํ•˜๋‚˜์˜ ์‹œ์ž‘์ ์—์„œ ๋ชฉํ‘œ ์ง€์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

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

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

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

์‹œ์ž‘์ง€๋กœ๋ถ€ํ„ฐ์˜ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•œ ์ตœ์†Œ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜์—ฌ

์‹œ์ž‘์ง€๋กœ๋ถ€ํ„ฐ ๋„์ฐฉ์ง€๊นŒ์ง€์˜ ์ตœ์†Œ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

            //๋ชฉ์ ์ง€ ์งํ–‰ , ํ˜„์žฌ๊นŒ์ง€ ๊ฑฐ๋ฆฌ + ํ˜„์žฌ์—์„œ ๋ชฉ์ ์ง€ ๊ฑฐ๋ฆฌ
            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]));
            }

๐Ÿ’ป ์ฝ”๋“œ

package problem.shortest.Q1916;

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_1916 {
    static final int INF = 987654321;
    static int V, E, start,end;
    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));

        V = Integer.parseInt(br.readLine());
        E = 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());
        }

        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));
        }
        String temp[] = br.readLine().split(" ");
        start = Integer.parseInt(temp[0]);
        end = Integer.parseInt(temp[1]);

        solve(start);


        if (distance[end] == INF) {
            System.out.println("INF");
        } else {
            System.out.println(distance[end]);
        }

    }

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

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

        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 October 22, 2020