알고리즘

백준 11779번 최소비용 구하기 2 (JAVA)

박카스마시며코딩 2021. 11. 30. 15:37

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

 

11779번: 최소비용 구하기 2

첫째 줄에 도시의 개수 n(1≤n≤1,000)이 주어지고 둘째 줄에는 버스의 개수 m(1≤m≤100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스

www.acmicpc.net

 

 

전형적인 다익스트라 문제이다. 저는 다익스트라를 통해 최소 값을 알아냈고, route라는 경로 배열을 통해 

이전 최소값의 경로들을 저장하였습니다. 

또한, Stack은 Vector로 구현되어 있어 동기화 기능이 내재되어 있어 효율적이지 않은 것으로 알고 있습니다.

그래서 deque를 통해 stack을 사용하였습니다.

 

package BOJ;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_11779 {
    static int map[][];
    static int n;
    static int m;
    static final int LIMIT = 100000;
    static int dij(int start, int end){
        boolean visited[] = new boolean[n+1];
        int route[] = new int[n+1];
        int distance[] = new int[n+1];
        Arrays.fill(distance,Integer.MAX_VALUE);
        distance[start] = 0;
        for(int i = 1 ; i <= n ; i++){
            int min = Integer.MAX_VALUE;
            int minIndex = 0;
            for(int j = 1 ; j <= n ; j++){
                if(!visited[j] && min > distance[j]){
                    min = distance[j];
                    minIndex = j;
                }
            }
//            System.out.println(minIndex);
            visited[minIndex] = true;
            if(minIndex == end){
                Deque<Integer> stack = new LinkedList<>();
                while(route[minIndex] != 0){
                    stack.addFirst(minIndex);
                    minIndex = route[minIndex];
                }
                stack.addFirst(minIndex);
                System.out.println(min);
                System.out.println(stack.size());
                while(!stack.isEmpty()){
                    System.out.print(stack.pollFirst()+" ");
                }
                System.out.println();
//                System.out.println(Arrays.toString(distance));
//                System.out.println(Arrays.toString(route));
                return min;
            }
//            System.out.println(minIndex);
//            System.out.println(Arrays.toString(distance));
            for(int j = 1 ; j <= n ; j++){
                if(!visited[j] && map[minIndex][j] != LIMIT && min + map[minIndex][j] <= distance[j]){
                    route[j] = minIndex;
                    distance[j] = min + map[minIndex][j];
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine()," ");
        Function<String,Integer> stoi = Integer::parseInt;
        n = stoi.apply(st.nextToken());
        st = new StringTokenizer(br.readLine()," ");
        m = stoi.apply(st.nextToken());
        map = new int[n+1][n+1];
        for(int i = 0 ; i <= n; i++){
            Arrays.fill(map[i],LIMIT);
        }
        for(int i = 0 ; i < m ; i++){
            st = new StringTokenizer(br.readLine()," ");
            int a = stoi.apply(st.nextToken());
            int b = stoi.apply(st.nextToken());
            int c = stoi.apply(st.nextToken());
            map[a][b] = Math.min(map[a][b],c);
        }
        st = new StringTokenizer(br.readLine()," ");
        int a = stoi.apply(st.nextToken());
        int b = stoi.apply(st.nextToken());
        dij(a,b);
    }
}

 

'알고리즘' 카테고리의 다른 글

ALGOSPOT - BOGGLE (java)  (0) 2021.12.01
백준 1103번 게임 (JAVA)  (0) 2021.12.01
백준 15900번 나무 탈출 (JAVA)  (0) 2021.11.29
백준 1058번 친구 (JAVA)  (0) 2021.11.28
백준 9656번 돌 게임 2 (JAVA)  (0) 2021.11.27