1916번: 최소비용 구하기

Joo·2022년 11월 15일

백준

목록 보기
21/113

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

문제

N개의 도시가 있다. 그리고 한 도시에서 출발하여 다른 도시에 도착하는 M개의 버스가 있다.

우리는 A번째 도시에서 B번째 도시까지 가는데 드는 버스 비용을 최소화 시키려고 한다. 

A번째 도시에서 B번째 도시까지 가는데 드는 최소비용을 출력하여라. 도시의 번호는 1부터 N까지이다.

입력

첫째 줄도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다.

그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다.

그리고 그 다음에는 도착지의 도시 번호가 주어지고 또 그 버스 비용이 주어진다. 버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수이다.

그리고 M+3째 줄에는 우리가 구하고자 하는 구간 출발점의 도시번호와 도착점의 도시번호가 주어진다.

출발점에서 도착점을 갈 수 있는 경우만 입력으로 주어진다.

출력

첫째 줄에 출발 도시에서 도착 도시까지 가는데 드는 최소 비용을 출력한다.

예제 입력 1

5
8
1 2 2
1 3 3
1 4 1
1 5 10
2 4 2
3 4 1
3 5 1
4 5 3
1 5

예제 출력 1

4

풀이

package shortest_path;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main_1916_dijkstra {

    private static int numberOfCity;
    private static int numberOfBus;
    private static int start;
    private static int destination;
    private static int[] minCost;
    private static ArrayList<City>[] adjacencyList;

    static class City {

        int number;
        int cost;

        public City(int number, int cost) {
            this.number = number;
            this.cost = cost;
        }

    }

    public static void main(String[] args) throws IOException {
        input();
        process();
        output();
    }

    private static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        numberOfCity = Integer.parseInt(br.readLine());
        numberOfBus = Integer.parseInt(br.readLine());

        adjacencyList = new ArrayList[numberOfCity + 1];
        minCost = new int[numberOfCity + 1];

        for (int i = 1; i <= numberOfCity; i++) {
            adjacencyList[i] = new ArrayList<>();
        }

        for (int i = 0; i < numberOfBus; i++) {
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int destination = Integer.parseInt(st.nextToken());
            int cost = Integer.parseInt(st.nextToken());

            adjacencyList[start].add(new City(destination, cost));
        }

        st = new StringTokenizer(br.readLine());
        start = Integer.parseInt(st.nextToken());
        destination = Integer.parseInt(st.nextToken());
    }

    private static void process() {
        dijkstra(start);
    }

    private static void dijkstra(int startCity) {
        PriorityQueue<City> queue = new PriorityQueue<>(Comparator.comparingInt(city -> city.cost));

        initMinCost();
        queue.add(new City(startCity, 0));

        while (!queue.isEmpty()) {
            City city = queue.poll();
            int cityNumber = city.number;
            int cityCost = city.cost;

            if (cityCost > minCost[cityNumber]) {
                continue;
            }

            for (City nextCity : adjacencyList[cityNumber]) {
                int nextCityNumber = nextCity.number;
                int newCost = minCost[cityNumber] + nextCity.cost;

                if (newCost >= minCost[nextCityNumber]) {
                    continue;
                }

                minCost[nextCityNumber] = newCost;
                queue.add(new City(nextCity.number, newCost));
            }
        }
    }

    private static void initMinCost() {
        for (int city = 1; city <= numberOfCity; city++) {
            minCost[city] = Integer.MAX_VALUE;

            if (city == start) {
                minCost[city] = 0;
            }
        }
    }

    private static void output() {
        System.out.println(minCost[destination]);
    }

}

0개의 댓글